gpt4 book ai didi

javascript - jQuery - 从包含 "../"的 URL 字符串中获取绝对路径

转载 作者:行者123 更新时间:2023-11-30 10:14:38 25 4
gpt4 key购买 nike

不知道之前有没有人回答过。我不知道要搜索什么。

我只想知道是否有针对以下问题的 jquery 函数。

假设我有一个这样的 URL 字符串 - www.example.com/a/b/../c我想获取 www.example.com/a/c

最佳答案

一种简单的方法是创建一个链接元素,设置它的 href 属性,然后读取它。

var a = document.createElement("a");
a.href = "http://www.example.com/a/b/../c";
var resolved = a.href;
console.log(resolved); // "http://www.example.com/a/c"

或者使用一些 jQuery:

var resolved = $("<a>").prop("href", "http://www.example.com/a/b/../c").prop("href");
console.log(resolved); // "http://www.example.com/a/c"

Live Example

这相对于当前文档的路径起作用,尽管对于上面的 URL 并不重要,因为 URL 是绝对的。

请注意,重要的是我们在这里使用的是反射属性,而不是属性。

关于javascript - jQuery - 从包含 "../"的 URL 字符串中获取绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24554160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com