使用jQuery获取当前URL?

JavaScript

SamL

2020-03-09

我正在使用jQuery。如何获取当前URL的路径并将其分配给变量?

范例网址:

http://localhost/menuname.de?foo=bar&number=0

第220篇《使用jQuery获取当前URL?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

19个回答
小哥达蒙 2020.03.09

In jstl we can access current url path using pageContext.request.contextPath, If you want to do a ajax call,

  url = "${pageContext.request.contextPath}" + "/controller/path"

Ex: in the page http://stackoverflow.com/questions/406192 this will give http://stackoverflow.com/controller/path

阿飞Sam 2020.03.09
var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
eva 2020.03.09

var path = location.pathname returns the path of the current URL (jQuery is not needed). The use of window.location is optional.

番长猴子古一 2020.03.09

Very Commonly Used top 3 ones are

1. window.location.hostname 
2. window.location.href
3. window.location.pathname
JinJinDavaid卡卡西 2020.03.09

See purl.js. This will really help and can also be used, depending on jQuery. Use it like this:

$.url().param("yourparam");
路易Green 2020.03.09

window.location will give you the current URL, and you can extract whatever you want from it...

神奇宝儿 2020.03.09

Use window.location.href. This will give you the complete URL.

西里小卤蛋 2020.03.09

The following are examples of useful code snippets that can be used – some of the examples use standard JavaScript functions and are not specific to jQuery:

See 8 Useful jQuery Snippets For URL’s & Querystrings.

阿飞宝儿猴子 2020.03.09

您可以使用js本身获取路径,window.location或者location为您提供当前URL的对象

console.log("Origin - ",location.origin);
console.log("Entire URL - ",location.href);
console.log("Path Beyond URL - ",location.pathname);

ItachiHarry 2020.03.09

如果有人要连接URL和哈希标记,请结合使用以下两个功能:

var pathname = window.location.pathname + document.location.hash;
.... 2020.03.09

这将使用JavaScript / jQuery返回当前页面的绝对URL

  • document.URL

  • $("*").context.baseURI

  • location.href

猴子西门 2020.03.09

您可以登录window.location并查看所有选项,仅使用URL:

window.location.origin

用于整个路径:

window.location.href

还有位置。_ _

.host
.hostname
.protocol
.pathname
Davaid番长十三 2020.03.09

这也将起作用:

var currentURL = window.location.href;
SamJinJin路易 2020.03.09

仅对于主机名,使用:

window.location.hostname
小卤蛋村村 2020.03.09

这是比许多人想象的更为复杂的问题。一些浏览器支持内置的JavaScript位置对象以及可通过window.location访问的关联参数/方法document.location但是,不同版本的Internet Explorer(6,7)不以相同的方式支持这些方法(不支持window.location.hrefwindow.location.replace()),因此您必须通过始终编写条件代码来手持Internet Explorer,以不同的方式访问它们。

因此,如果您有可用的jQuery并已加载,则最好使用jQuery(位置),就像其他人提到的那样,因为它可以解决这些问题。但是,如果您正在做一个示例-通过JavaScript进行客户端地理定位重定向(即使用Google Maps API和location对象方法),那么您可能不想加载整个jQuery库并编写条件代码,检查Internet Explorer / Firefox / etc等的每个版本。

Internet Explorer使前端编码的猫感到不高兴,但是jQuery却是一堆牛奶。

路易Eva 2020.03.09

只需在JavaScript中添加此函数,它将返回当前路径的绝对路径。

function getAbsolutePath() {
    var loc = window.location;
    var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
    return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
}

我希望这个对你有用。

null 2020.03.09

您将要使用JavaScript的内置window.location对象。

Eva猴子古一 2020.03.09

如果您需要URL中存在的哈希参数,则window.location.href可能是一个更好的选择。

window.location.pathname
=> /search

window.location.href 
 => www.website.com/search#race_type=1
Stafan村村达蒙 2020.03.09

要获取路径,可以使用:

var pathname = window.location.pathname; // Returns path only (/path/example.html)
var url      = window.location.href;     // Returns full URL (https://example.com/path/example.html)
var origin   = window.location.origin;   // Returns base URL (https://example.com)

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android