weixin_33747129 2014-03-05 14:55 采纳率: 0%
浏览 611

设置全局基本URL

I'm using jQuery: which is the best way to store base url value for AJAX requests, in a way that every scripts can somehow reference it without manual changes for every update? Should I store it in window object, html, global variable, or what? Many thanks in advance.

  • 写回答

2条回答 默认 最新

  • weixin_33713350 2014-03-05 15:03
    关注

    You could create function to do it automatically:

    var BASE_URL = 'http://www.foo.com/url-root/';
    
    function getUrl(url) {
        return BASE_URL.concat(url);
    }
    

    Now, whenervar you want a url...

    var url = getUrl('bar.php');  //returns 'http://www.foo.com/url-root/bar.php'
    

    I you don't want it to be global...

    (function() {
        var BASE_URL = 'http://www.foo.com/url-root/';
    
        window.getUrl = function(url) {
            return BASE_URL.concat(url);
        }
    })();
    

    Or you could create a object.

    评论

报告相同问题?