Tips for using js in frameworks

Hello forums!!
Here I am going to share the tips for using framework variables in Javascript codes.

suppose say we have one js code which requires configuration for base url, images etc.
Configuring the JS code every-time while switching the server may be time consuming and hence overhead too.

Here is some tricks to tackle such (I know most of us are very familiar with such techniques,
but the newbie will get the ideas, isn’t that great?)

Technique #1>
use php’s header()
example.js.php (for this you have to make the php parseable file)

<?php
header("content-type: application/x-javascript");
//js code goes here..
?>
var example_js_url 	= '<?php echo FRAMEWORK_JS_URL; ?>'; //no need to hardcode here will work in all server
var example_image_url 	= '<?php echo FRAMEWORK_IMAGE_URL; ?>';//""
.....

and including this file as normal js as:

<script type="text/javascript" src="example.js.php"></script>

Technique #2>
Define js global variable before including the js file.

<head>
<script type="text/javascript">
var CONFIG_EXAMPLE_JS_URL 	= '<?php echo FRAMEWORK_JS_URL; ?>';
var CONFIG_EXAMPLE_IMAGE_URL	= '<?php echo FRAMEWORK_IMAGE_URL; ?>';	
</script>
<script type="text/javascript" src="example.js"></script>
</head>

example.js will look like:

.....
var example_js_url 	= CONFIG_EXAMPLE_JS_URL;	
var example_image_url 	= CONFIG_EXAMPLE_IMAGE_URL;
.....

Hope I am able to share my idea.
If not, PHPycho is there for share :slight_smile:

Also you can share your ideas too.

Thanks

isn’t the post useful?

I didn’t get any responses from our beloved users.

thanks

Any inputs from our forumians?

Nobody replied.
I was unable to know whether my sharing was good enough or not.
I were going to share again some tips but this really disappointed me.

Nobody mentioned that whether i am doing right or wrong, or can be done in that way… bla…bla…
:frowning:

It seems that those are the only obvious solutions for such. I have used the second method for many projects before.

I have used the first of those for a number of projects where I needed to pass info into the script from the server.