开发者

Javascript - How can I check to see if a url is loosely equal to the one in the location bar?

开发者 https://www.devze.com 2023-02-25 19:41 出处:网络
function isSameUrl(url){ /*what goes here?*/ } If the current url is http://www.example.com/test#bar, the following would return true:

function isSameUrl(url){ /*what goes here?*/ }

If the current url is http://www.example.com/test#bar, the following would return true:

 isSameUrl('#bar')
 isSameUrl('/test#bar')
 isSameUrl('http://www.example.com/test#bar')
开发者_StackOverflow

Basically, if the location bar would not change if a link with the specified url was clicked, then it should return true.


You need to parse the URI reference before comparing it. You can use this regular expression to parse URI references (slightly modified):

/^([^:\/?#]+:)?(?:\/\/([^\/?#]+))?([^?#]+)?(\?[^#]*)?(#.*)?/

Then you can compare each component that is not undefined to the corresponding component in the location object:

function isSameUrl(url, location) {
    location = location || document.location;
    var match = url.match(/^([^:\/?#]+:)?(?:\/\/([^\/?#]+))?([^?#]+)?(\?[^#]*)?(#.*)?/);
    if (typeof match[1] === "string" && match[1].length > 0 && match[1].toLowerCase() !== location.protocol) return false;
    if (typeof match[2] === "string" && match[2].length > 0 && match[2].replace(new RegExp(":("+{"http:":80,"https:":443}[location.protocol]+")?$"),"") !== location.host) return false;
    if (typeof match[3] === "string" && match[3].length > 0 && match[3] !== location.pathname) return false;
    if (typeof match[4] === "string" && match[4].length > 0 && match[4] !== location.search) return false;
    if (typeof match[5] === "string" && match[5].length > 0 && match[5] !== location.hash) return false;
    return true;
}


For an exact match

var isSameUrl = function(url) {
    return window.location.href == url;
}

and for your needs

changed for query string support

var isSameUrl = function(url) {
  var path = function(s) { return s.replace(/\?.*$/,''); };
  return path(window.location.href) == url ||
         path(window.location.pathname + window.location.hash) == url ||
         window.location.hash == url;
}


In modern browsers, the value of the location bar is stored in a Location object which you can access using the window.location property. It has various conveniences for accessing various parts of the URL, but the portion of the URL after the domain name can be found in: window.location.pathname. The portion after # can be found in window.location.hash (but only in the most recent browsers). The portion after ? can be found in window.location.search -- and so on.


If you want to see if a string is included in your URL, you can use this:

function isSameUrl(test) {
    return window.location.href.indexOf(test) != -1;
}


I think isSame (url) would be something like this:

function isSame(url){
    return window.location.indexOf(url) > 0
}


You need to build a regular expression based off 'url' parameter of your function, then test the current url against that regular expression. See http://www.w3schools.com/jsref/jsref_obj_regexp.asp for the details on creating the regular expression and on using the test() method to check for matches. There are many good regular expression testers online as well.

0

精彩评论

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