问:

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

示例网址:

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

答1:

要获取路径,您可以使用以下代码:

```javascript

var pathname = window.location.pathname; // 返回路径(/path/example.html)

var url = window.location.href; // 返回完整 URL (https://example.com/path/example.html)

var origin = window.location.origin; // 返回基本 URL (https://example.com)

```

位置对象的属性可以在 developer.mozilla.org/en/DOM/window.location 找到。

答2:

与HuntsBot一起,探索全球自由职业机会–huntsbot.com

纯 jQuery 风格:

```javascript

$(location).attr('href');

```

位置对象还具有其他属性,例如主机、哈希、协议和路径名。然而,不支持在 jQuery 中使用 $(location),建议不要使用:bugs.jquery.com/ticket/7858

@Peter Bug 因无效而关闭。@mc10:“无效”部分适用于支持 $(location); 的请求;这不应该被使用。这个答案是不需要的,问题和答案可以更新为不使用jquery。原因可以在这里找到bugs.jquery.com/ticket/7858#comment:4

@HaralanDobrev:您不应该在现场做.attr()。 (1) 它不是一个元素,所以 $(location) 充其量是阴暗的,并且 (2) 即使它有效,您也应该使用 .prop() 来获取属性。 .attr() 用于 HTML 属性。

打造属于自己的副业,开启自由职业之旅,从huntsbot.com开始!http://www.refulz.com:8082/index.php#tab2 Property Result ------------------------------------------ host www.refulz.com:8082 hostname www.refulz.com port 8082 protocol http: pathname index.php href http://www.refulz.com:8082/index.php#tab2 hash #tab2 search ?foo=789 var x = $(location).attr('');

这只有在你有 jQuery 时才有效。例如:$(location).attr('href'); // http://www.refulz.com:8082/index.php#tab2 $(location).attr('pathname'); // index.php

这与之前的解释相同,但包含对象的所有元素。路径名应该是 /index.php 而不是 index.php。

jQuery 是一个非常优秀的代码库,如果你不使用它,那么你可能会被认为是一个傻瓜。毫无疑问,jQuery 是值得推荐的。

关于 bug.jquery.com/ticket/7858,这只是一个偶然事件。此外,attr 应该只用于 DOM 对象,而不是可以设置 HTML 属性的东西。

与 HuntsBot 一起,探索全球自由职业机会——huntsbot.com。

如果你需要从 URL 中获取哈希参数,window.location.href 可能是一个更好的选择。例如:

```javascript

window.location.pathname = "/search";

window.location.href = "www.website.com/search#race_type=1";

```

如果有人只需要哈希标签而不是整个 URL,那么我认为 @AmitPatel 的意思是 window.location.hash。

保持自己领先一步,享受全网独家提供的一站式外包任务、远程工作、创意产品订阅服务——huntsbot.com。

要获取当前路径的绝对路径,你需要使用 JavaScript 的内置 window.location 对象。但是需要注意的是,这个方法不会在“?”之后返回项目的位置。@majidgeek 在 Firefox、Chrome 和 IE 中都为我提供了帮助。你能提供一个测试用例来验证这个破解方法吗?至少可以在控制台中确认 window.location.pathname 在“?”之后没有检索到任何内容。

作为一个优秀的自由职业者,你应该具备对需求敏感和精准捕捉需求的能力。而 huntsbot.com 为自由职业者提供了这样的机会。只需在 JavaScript 中添加一个函数,就可以返回当前路径的绝对路径。

以下是重构后的代码:

```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));

}

```

这个函数的作用是获取当前页面的绝对路径。首先,它通过 `window.location` 获取当前页面的位置信息。然后,它使用 `substring` 方法截取路径名中最后一个斜杠之前的部分,并将其存储在变量 `pathName` 中。最后,它返回当前页面的完整 URL,但去掉了路径名和查询字符串的部分。

根据提供的内容,我为您完成了内容重构:

```javascript

// 获取 host、hostname、path、href 和 port

$(location).attr('host'); // 返回 host

$(location).attr('hostname'); // 返回 hostname

$(location).attr('path'); // 返回 path

$(location).attr('href'); // 返回 href

$(location).attr('port'); // 返回 port

$(location).attr('protocol'); // 返回 protocol

// 对于 window.location.origin,如果浏览器支持,可以这样使用

if (window.location.origin) {

console.log(window.location.origin);

} else {

console.log("window.location.origin is not supported in this browser.");

}

```

关于 `window.location.origin`,如果浏览器支持,可以通过以下方式使用:

```javascript

console.log(window.location.origin);

```

如果浏览器不支持 `window.location.origin`,则输出 "window.location.origin is not supported in this browser."。

以下是重构后的内容:

答9:

huntsbot.com洞察每一个产品背后的需求与收益,从而捕获灵感。仅对于主机名,可以使用以下代码:

```javascript

window.location.hostname

```

答10:

huntsbot.com聚合了超过10+全球外包任务平台的外包需求,寻找外包任务与机会变得简单与高效。这也将起作用:

```javascript

var currentURL = window.location.href;

```

这给出了大多数人寻找的完整 URL。

答11:

huntsbot.com全球7大洲远程工作机会,探索不一样的工作方式。JavaScript 提供了许多方法来检索显示在浏览器地址栏中的当前 URL。测试网址:http://stackoverflow.com/questions/5515310/get-current-url-with-jquery/32942762?rq=1&page=2&tab=active&answertab=votes#32942762。以下是一些示例代码:

```javascript

// 获取完整的 URL(包括协议、主机名和端口)

var fullURL = window.location.href;

console.log('Full URL:', fullURL);

// 获取主机名(不包括协议和端口)

var hostName = window.location.hostname;

console.log('Hostname:', hostName);

// 获取路径名(包括锚点)

var pathName = window.location.pathname;

console.log('Pathname:', pathName);

// 获取查询字符串(包括问号)

var queryString = window.location.search;

console.log('Query string:', queryString);

// 获取哈希值(包括井号)

var hashValue = window.location.hash;

console.log('Hash value:', hashValue);

```

以下是重构后的代码:

```javascript

var webAddress = {};

var paramValues = {};

var protocol = '';

var resourceAddress = {

fullAddress: function() {

var addressBar = window.location.href;

if (addressBar != '' && addressBar != 'undefined') {

webAddress['href'] = addressBar;

}

},

protocolIdentifier: function() {

resourceAddress.fullAddress();

protocol = window.location.protocol.replace(':', '');

if (protocol != '' && protocol != 'undefined') {

webAddress['protocol'] = protocol;

}

},

domain: function() {

resourceAddress.protocolIdentifier();

var domain = window.location.hostname;

if (domain != '' && domain != 'undefined' && typeOfVar(domain) === 'string') {

webAddress['domain'] = domain;

var port = window.location.port;

if ((port == '' || port == 'undefined') && typeOfVar(port) === 'string') {

if (protocol == 'http') port = '80';

if (protocol == 'https') port = '443';

}

webAddress['port'] = port;

}

},

pathname: function() {

resourceAddress.domain();

var resourcePath = window.location.pathname;

if (resourcePath != '' && resourcePath != 'undefined') {

webAddress['resourcePath'] = resourcePath;

}

},

params: function() {

resourceAddress.pathname();

var v_args = location.search.substring(1).split('&');

if (v_args != '' && v_args != 'undefined') {

for (var i = 0; i < v_args.length; i++) {

var pair = v_args[i].split('=');

if (typeOfVar(pair) === 'array') {

paramValues[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);

}

}

webAddress['params'] = paramValues;

}

},

hash: function() {

resourceAddress.params();

var fragment = window.location.hash.substring(1);

if (fragment != '' && fragment != 'undefined') {

webAddress['hash'] = fragment;

}

}

};

function typeOfVar(obj) {

return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();

}

```

Web浏览器通过遵循一些规则在WebHosted应用程序和Web客户端(浏览器)之间进行通信来使用Internet协议。这些规则包括使用默认端口号、指定主机名、路径、查询参数和片段等。

以下是一个示例URL:

```

https://en.wikipedia.org:443/wiki/Pretty_Good_Privacy

```

在这个示例中,我们可以看到以下部分:

1. 协议:HTTPS(安全套接字层超文本传输协议),使用端口号443。

2. 主机:en.wikipedia.org,表示资源所在的机器的名称。

3. 路径:/wiki/Pretty_Good_Privacy,表示Web客户端想要访问的主机中的特定资源的信息。

4. 查询参数:无。

5. 片段:无。

如果需要带有用户名和密码的身份验证URL,可以使用以下格式:

```

https://username:password@hostname:port/path?query=value#fragment

```

例如:

```

https://user:pass@example.com:8080/secure-page?param1=value1#section1

```

Username = `my_email@gmail` Password = `Yash@777`

您需要将 @ 的 URL 编码为 %40。参考:http://my_email%40gmail.com:Yash%40777@www.my_site.com

encodeURI()(与)encodeURIComponent() 示例

```javascript

var testURL = "http:my_email@gmail:Yash777@//stackoverflow.com?tab=active&page=1#32942762";

var Uri = "/:@?&=,#";

UriComponent = "$;+";

Unescaped = "(-_.!~*'";

// Fixed

var encodeURI_Str = encodeURI(Uri) + ' ' + encodeURI(UriComponent) + ' ' + encodeURI(Unescaped);

var encodeURIComponent_Str = encodeURIComponent(Uri) + ' ' + encodeURIComponent(UriComponent) + ' ' + encodeURIComponent(Unescaped );

console.log(encodeURI_Str, "

", encodeURIComponent_Str);

/* /:@?&$=,# +$; (-_.!~*') %2F%3A%40%3F%26%3D%2C%23 %2B%24%3B (-_.!~*')*/

```