根据base.href或位置生成规范/真实URL

发布于 2024-09-01 10:18:27 字数 660 浏览 9 评论 0原文

是否有方法/函数可以获取规范/转换后的 URL,并尊重页面的任何 base.href 设置?

我可以通过(在 jQuery 中)使用 $("base").attr("href") 获取基本 URL,并且我可以使用字符串方法来解析与此相关的 URL,但是

  • $("base").attr("href") 没有主机、路径等属性(就像 window.location 有),
  • 手动将它们放在一起是相当乏味的

,例如,给定 "http:// /example.com/foo/" 和相对 URL "/bar.js",结果应该是:"http://example.com/bar.js "

如果 base.href 不存在,则 URL 应相对于 window.location。 这应该处理不存在的base.href(在这种情况下使用位置作为基础)。

是否已经有一个标准方法可用于此?

(我正在寻找这个,因为当使用像“/foo.js”这样的相对 URL 并且使用 BASE 标签时 jQuery.getScript 失败(FF3.6 发出 OPTIONS 请求,nginx 无法处理这个)。当使用完整 URL(base.href.host +“/foo.js”,它有效)。)

Is there a method/function to get the canonical / transformed URL, respecting any base.href setting of the page?

I can get the base URL via (in jQuery) using $("base").attr("href") and I could use string methods to parse the URL meant to being made relative to this, but

  • $("base").attr("href") has no host, path etc attributes (like window.location has)
  • manually putting this together is rather tedious

E.g., given a base.href of "http://example.com/foo/" and a relative URL "/bar.js", the result should be: "http://example.com/bar.js"

If base.href is not present, the URL should be made relative to window.location.
This should handle non-existing base.href (using location as base in this case).

Is there a standard method available for this already?

(I'm looking for this, since jQuery.getScript fails when using a relative URL like "/foo.js" and BASE tag is being used (FF3.6 makes an OPTIONS request, and nginx cannot handle this). When using the full URL (base.href.host + "/foo.js", it works).)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-09-08 10:18:27

这能起到作用吗?

function resolveUrl(url){
    if (!url) {
        throw new Error("url is undefined or empty");
    }
    var reParent = /[\-\w]+\/\.\.\//, // matches a foo/../ expression 
 reDoubleSlash = /([^:])\/\//g; // matches // anywhere but in the protocol
    // replace all // except the one in proto with /
    url = url.replace(reDoubleSlash, "$1/");
    var base = (document.getElementsByTagName('BASE')[0] && document.getElementsByTagName('BASE')[0].href) || "";

    // If the url is a valid url we do nothing
    if (!url.match(/^(http||https):\/\//)) {
        // If this is a relative path
        var path = (url.substring(0, 1) === "/") ? base : location.pathname;
        if (path.substring(path.length - 1) !== "/") {
            path = path.substring(0, path.lastIndexOf("/") + 1);
        }
        if (!url.match(/^(http||https):\/\//)) {
            url = location.protocol + "//" + location.host + path + url;
        }
    }

    // reduce all 'xyz/../' to just '' 
    while (reParent.test(url)) {
        url = url.replace(reParent, "");
    }
    return url;
}

它是根据我周围的一些代码修改的,因此尚未经过测试

Does this do the trick?

function resolveUrl(url){
    if (!url) {
        throw new Error("url is undefined or empty");
    }
    var reParent = /[\-\w]+\/\.\.\//, // matches a foo/../ expression 
 reDoubleSlash = /([^:])\/\//g; // matches // anywhere but in the protocol
    // replace all // except the one in proto with /
    url = url.replace(reDoubleSlash, "$1/");
    var base = (document.getElementsByTagName('BASE')[0] && document.getElementsByTagName('BASE')[0].href) || "";

    // If the url is a valid url we do nothing
    if (!url.match(/^(http||https):\/\//)) {
        // If this is a relative path
        var path = (url.substring(0, 1) === "/") ? base : location.pathname;
        if (path.substring(path.length - 1) !== "/") {
            path = path.substring(0, path.lastIndexOf("/") + 1);
        }
        if (!url.match(/^(http||https):\/\//)) {
            url = location.protocol + "//" + location.host + path + url;
        }
    }

    // reduce all 'xyz/../' to just '' 
    while (reParent.test(url)) {
        url = url.replace(reParent, "");
    }
    return url;
}

It is modified from some code I had around, so it hasn't been tested

柒七 2024-09-08 10:18:27

js-uri 是一个有用的 javascript 库,可以实现此目的:http://code.google.com/ p/js-uri/

您仍然需要处理 base.href 与 window.location 部分,但其余部分可以通过库完成。

js-uri is a useful javascript library to accomplish this: http://code.google.com/p/js-uri/

You would still need to handle the base.href vs window.location parts, but the rest could be accomplished with the library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文