views:

254

answers:

4

My baseUrl path for jQuery ajax requests is different in development and production mode. How can I set it in some kind of config.ini file to switch it easily. Rightnow I'm using a baseurl.js file containing just baseurl path. In production mode, I change this variable via shell script.

What should be better approach?

edit

To make things little clearer,

 $.ajax({
   type: "GET",
   url: myurl,
   dataType: "script"
 });

Here 'myurl' var is different for production and development.

PS: My question is not about version control or packing files into one.

A: 

Using a revision control system would be the best approach.

You would have one configuration file, and each client can "check it out" and modify it at will to suit their own environment (development, testing, production, etc).

You'll probably have the production configuration committed to your revision control system, and you'll have a customized version in your development environment. Just don't check your customized version in to the revision control server.

I don't know what operating system you're on, but I would recommend TortoiseSVN (for Windows) and Beanstalk (hosted subversion service) to get up and running with minimal effort. If you'd rather setup your own SVN server, VisualSVN makes it a trivial task.

And voila, no more swapping configurations!

Dolph
The chance of committing the dev config file into the production tree is too great. Besides, if everything suddenly broke, how soon would you know where to look? I'd probably look in all the wrong places first.The best solution is to put this in a build script. If you're using some kind of compressor (as stated in another answer), you're halfway there already.
Kris Walker
So, commit the dev config file, and let the production environment version be the customized copy.
Dolph
+1  A: 

When deploying any sites that use more than I little JavaScript, I like to use YUI Compressor to pack all of my script files down in to a single file which then gets included on the production site, and will load much faster than if the browser had to load each file individually.

To avoid making mistakes when running it, I created a shell script containing the command line arguments for YUI, which gets run before deployment. This is also the perfect place to reference any production-specific JavaScript files - for example you could create a production.js file that sets your baseurl path and include it in the command line.

Tobias Cohen
A: 

If you are using some dynamic pages technology (asp, php, ...) then you could turn your baseurl.js file to a dynamic one like baseurl.js.php and inside it print the current url . (if that is what you are asking).

Gaby
@Gaby: to make this work do I need to add php handler for js files in Apache .htaccess? If yes, then all js files would be parsed as php, which would be slow.
understack
no just rename the `baseurl.js` to `baseurl.js.php` and include that in your code.. like `<script type="text/javascript" src="/path/to/baseurl.js.php"></script>`
Gaby
I can't upvote this. It seems a bug.
understack
no worries.. its the thought that counts ;)
Gaby
A: 

I think that having relative URLs be different in development versus production sounds like a mistake anyway. That's pretty much the advantage of relative URLs: they navigate your application structure in a way that's independent of host deployment details.

Pointy