开发者

Url in jQuery Scripts for different environments?

I have 3 environments - development, stage and prod. I rely on jQuery for some ajax display. So I have something like that in my development:

   function needsForcedReset() {
           $.get("http://dev/_layouts/Ajax/DoesUserNeedForceResetPassword.aspx", function(result) {

        alert('got the data from force password ajax page: ' + result);

        if (result.toString().length > 0) {                
            showAlert('Your have to change your domain password now', 0, 
                false, false, notificationClass.fail);

            return true;
        }
        return false;
    });
    return false;

   }

So the point of interest is the hardcoded link inside the $.get fun开发者_如何学Pythonction. How do I code with jQuery to make that link relative to the path of _layouts/ajax or code in the way that I dont have to specify the host for the url for each different deploy scenario?


Just drop the http://dev/ and use a server-relative url.


There's multiple ways to do this. Here's two:

  1. Get the current document location through JS with window.location.href (for the full URL) or window.location.host (for the hostname + port). There's more variations of these, Google it.

  2. Set a html <base href="site.com" /> tag in the head section through your serverside programming logic (usually available in a constant for many CMS's or frameworks) and then extract it with jQuery: $('base') ... more info here: http://www.w3schools.com/TAGS/tag_base.asp

Cheers :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜