【问题标题】:How to get current url along with query string in jQuery [duplicate]如何在jQuery中获取当前url以及查询字符串[重复]
【发布时间】:2018-02-14 13:45:30
【问题描述】:

我想要当前的 url 路径及其查询字符串。

我在下面尝试过,但没有得到想要的输出。

var pathname = window.location.pathname; // Returns path only
var url = window.location.href;

假设我的网址是http://localhost:1111/Contact/Index?text=aa

注意:这里查询字符串是可选的,如果没有查询字符串,那么它应该只提供路径名。

我想要 /Contact/Index?text=aa,TIA。

【问题讨论】:

  • 我得到了答案它的 window.location.pathname+window.location.search.substr(1);
  • 感谢大家的努力和时间..
  • window.location.search 会为你做这件事。

标签: javascript jquery


【解决方案1】:

在 jQuery 中你可以这样做

$(location).attr('href')

位置对象还包含属性,例如主机、哈希、协议和路径名。

【讨论】:

  • 那么,你为什么要在location.href上使用它?
  • 因为他要求jQuery
  • 如果我问一种用工业钢炉烤蛋糕的方法,你不会告诉我用普通烤箱吗?
  • 不,我不会。因为我会很好奇蛋糕的味道。
  • 可能相当“干得好”。
【解决方案2】:

window.location.pathname 将为您提供路径或网址。

【讨论】:

  • 它不返回查询字符串,他也想要那个
猜你喜欢
  • 2016-12-31
  • 2021-07-19
  • 2018-09-04
  • 2013-04-18
  • 2011-06-07
  • 2012-04-09
  • 2013-11-21
  • 1970-01-01
相关资源
最近更新 更多