Javascript-如何检查url是否与位置栏中的url大致相等?

Javascript-如何检查url是否与位置栏中的url大致相等?,javascript,jquery,Javascript,Jquery,函数isSameUrl(url){/*这里是什么?*/} 如果当前url为http://www.example.com/test#bar,以下将返回true: isSameUrl('#bar') isSameUrl('/test#bar') isSameUrl('http://www.example.com/test#bar') 基本上,如果单击具有指定url的链接时位置栏不会更改,那么它应该返回true。在现代浏览器中,位置栏的值存储在位置对象中,您可以使用窗口.location属性访

函数isSameUrl(url){/*这里是什么?*/}

如果当前url为
http://www.example.com/test#bar
,以下将返回true:

 isSameUrl('#bar')
 isSameUrl('/test#bar')
 isSameUrl('http://www.example.com/test#bar')

基本上,如果单击具有指定url的链接时位置栏不会更改,那么它应该返回true。

在现代浏览器中,位置栏的值存储在位置对象中,您可以使用
窗口.location
属性访问该对象。它为访问URL的各个部分提供了各种便利,但域名后的URL部分可以在以下位置找到:
window.location.pathname
#
后面的部分可以在
窗口.location.hash中找到(但仅在最新的浏览器中)。
后面的部分可以在
窗口、位置、搜索中找到,依此类推。

查找精确匹配

var isSameUrl = function(url) {
    return window.location.href == url;
}
为了你的需要

已更改查询字符串支持

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;
}

如果要查看URL中是否包含字符串,可以使用以下选项:

function isSameUrl(test) {
    return window.location.href.indexOf(test) != -1;
}
我认为ISNAME(url)应该是这样的:

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

您需要基于函数的“url”参数构建一个正则表达式,然后根据该正则表达式测试当前url。有关创建正则表达式和使用test()方法检查匹配项的详细信息,请参阅。在线上也有很多优秀的正则表达式测试人员。

您需要在比较URI引用之前解析它。您可以使用(稍微修改):

然后,您可以将未定义的每个组件与
位置
对象中的相应组件进行比较:

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;
}

严格来说,
www.example.com/test#bar
由相对路径
www.example.com/test
和片段
bar
组成;从
http://www.example.com/test#bar
它将导致
http://www.example.com/www.example.com/test#bar
@gumbo-你说得对,我将从示例中选取一个。新建RegExp(url.test)(document.location)怎么样
如果url为
/test
且当前url为
www.example.com/subfolder/test
,则不适用。如果url为
/test
且当前位置为
http://www.example.com/subfolder/test
@James谢谢,我将对其进行一点测试,看看它是否遗漏了任何边缘情况。是否要匹配查询参数或在匹配中忽略它们?基本上,只要在单击带有url的链接时位置栏不会更改,我就会返回true。我知道,更改为删除查询字符串,这是假设您的链接都没有查询字符串,如果有,您可以将它们添加到问题列表中吗?您的观点是有道理的——然后使用其他资源了解正则表达式的详细信息。但是用一个正则表达式来解决这个问题——这就是这个问题的答案。你知道一个正则表达式可以解决这个问题吗?
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;
}