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.
解决 无用评论 打赏 举报