javascript - if 语句不起作用 jquery

标签 javascript jquery if-statement pathname

我有这个网址:

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/

相关文章:

php - JavaScript 和 PHP 参数

javascript - 为什么添加原型(prototype)的不同方式会影响?

javascript - 如何使用 jquery 在克隆表单上显示日历

haskell - haskell中一系列独立的if语句

if-statement - Svelte 中是否有更好的方法来包含条件 HTML 元素

r - 使用 if 语句对数字进行分类的循环

javascript - 保持按钮处于选中状态

php - 查询字符串是如何工作的

javascript - 从服务器获取文件并将其转换为javascript中的字节数组?

javascript - 如何使用 javascript 仅刷新表中的数据?