我有这个网址:
csumb/index.php?page=consultanta
我尝试比较 2 个链接,但如果我更改链接并刷新页面,此代码会执行相同的操作。
var pathname = window.location.pathname;
var a = "csumb/index.php?page=consultanta";
if(pathname == a) {
$("body").html("rahat");
}
最佳答案
location
有多个部分(或 window.location
,使用文字引用):
https://packagist.org/search/?search_query%5Bquery%5D=symfony
直接使用 console.log(location)
进入 Chrome 控制台,它会提供以下属性(以及一些其他内容):
hash: ""
host: "packagist.org"
hostname: "packagist.org"
href: "https://packagist.org/search/?search_query%5Bquery%5D=symfony&page=4"
origin: "https://packagist.org"
pathname: "/search/"
port: ""
protocol: "https:"
search: "?search_query%5Bquery%5D=symfony&page=4"
你真正追求的是:
var pathname = location.pathname + location.search;
var a = "/csumb/index.php?page=consultanta";
// ^ Note the / at the beginning
文件名(如果在 URL 中)将位于 location.pathname
中,因此也不需要单独添加 index.php
。
关于javascript - if 语句不起作用 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14292589/