【问题标题】:making relative url absolute using arbitrary root in javascript在javascript中使用任意根使相对url绝对
【发布时间】:2015-04-03 21:45:37
【问题描述】:

假设我在不同域 (mydomain.com) 上的页面上,并且相对 url 仅存在于代码中(不在 DOM 中)

如何在 javascript 中完全组合两个任意 url?

var a = 'http://example.com/some/path/';
var b = '../other/path/';
var c = magicUrlCombine(a,b);
assert(c == 'http://example.com/some/other/path/');

它也应该适用于

var a = 'http://example.com/some/path/';
var b = 'http://pink-unicorns.com/some/other/path/';
var c = magicUrlCombine(a,b);
assert(c == 'http://pink-unicorns.com/some/other/path/');

编辑:

我正在寻找一个完全通用的函数,用于将绝对 url 与任意 url 结合起来。与浏览器用于解析链接的逻辑相同,但用于不在页面 HTML 中和/或与当前 location.href 无关的 url。

var a = 'http://example.com/a/b/c/';
var b = '../d/e/';
assert(c == 'http://example.com/a/b/d/e/')

var b = '/f/g/';
assert(c == 'http://example.com/f/g/')

var b = 'http://jquery.com/h/i/';
assert(c == 'http://jquery.com/h/i/')

编辑 2:

node.js 有一个 url module 具有正确的功能,但我还没有找到在客户端重用它的好方法。 (how to use node.js module system on the clientside)

我设法破解了我的方法,让它运行起来,但这并不是我觉得将其放入生产站点的真正解决方案。 Hackety hack

【问题讨论】:

  • 为了澄清,我正在寻找 msdn.microsoft.com/en-us/library/ay1kx93s.aspx 的等价物 a 是绝对的,b 是绝对的或相对的,c 是绝对的。
  • 您能否用实际示例更新您的示例,其中基础和相对路径中没有重复路径?或许我的回答还是正确的。

标签: javascript url


【解决方案1】:

JQuery Mobile 有它

$.mobile.path.makeUrlAbsolute(relPath, absPath)

console.log($.mobile.path.makeUrlAbsolute('../d/e/', 'http://example.com/a/b/c/'));
console.log($.mobile.path.makeUrlAbsolute('/f/g/', 'http://example.com/a/b/c/'));
console.log($.mobile.path.makeUrlAbsolute('http://jquery.com/h/i/', 'http://example.com/a/b/c/'));

都给出了预期的结果

【讨论】:

    【解决方案2】:

    我在服务器端用过,用的是NodeJS,

    var url = require('url');
    url.resolve(from, to);
    

    在你的情况下:

    var a = 'http://example.com/some/path/';
    var b = '../other/path/';
    var c = url.resolve(a, b);
    assert(c == 'http://example.com/some/other/path/');
    
    var a = 'http://example.com/some/path/';
    var b = 'http://pink-unicorns.com/some/other/path/';
    var c = url.resolve(a, b);
    assert(c == 'http://pink-unicorns.com/some/other/path/');
    

    【讨论】:

      【解决方案3】:

      忍不住想解决办法

      var magicUrlCombine = function(a,b){
         return (a + b).replace(/[\w\-\.]+\/..\/|\:\/\/[\w\-\.\/]+http/g,'');
      }
      

      适用于测试用例和两者的组合

      http://jsfiddle.net/8HLeQ/2/

      【讨论】:

      • 可能在狭窄的情况下工作,不是通用解决方案。任何带有 2 字符路径元素的 URL 都失败(您没有转义 '..')。当您从“path/start.html”重定向到“finish.html”时,自定义端口、https 和简单情况下的服务器也会失败。但不错的尝试:-)
      【解决方案4】:

      我以为我理解了这个问题,但我的小提琴返回两个错误。例子不明显

      http://jsfiddle.net/mplungjan/z5SUn/

      function magicUrlCombine(a,b) {
        var linkA = document.createElement('a');
        linkA.href = a;
        var linkB = document.createElement('a');
        linkB.href = b;
        return linkB.href.replace(linkB.host,linkA.host)
      }
      

      【讨论】:

      • 只有在链接是相对于当前页面的情况下(排序)才有效。我正在寻找一个完全通用的解决方案
      • @Jesper - 听起来你需要 FreakTM 的解决方案
      【解决方案5】:

      这是一个可能的但未经测试的解决方案:

      function magicCombine(a,b){
          if(b.indexOf('://') != -1) return b;
      
          var backs = 0;
          var lastIndex = b.indexOf('../');
      
          while(lastIndex != -1){
              backs++;
              lastIndex = b.indexOf('../', lastIndex+3);
          }
      
          var URL = a.split('/');
          //Remove last part of URL array, which is always either the file name or [BLANK]
          URL.splice(URL.length-1, 1)
      
          if(b.substr(0,1) == '/')
              b = b.substr(1);
          var toAdd = b.split('/');
      
          for(var i = 0, c = toAdd.length-backs; i < c; ++i){
              if(i < backs)
                  URL[URL.length - (backs-i)] = toAdd[backs+i];
              else
                  URL.push(toAdd[backs+i]);
          }
      
          return URL.join('/');
      }
      

      应该同时处理这两种情况...

      【讨论】:

      • 嗯,为什么magicCombine('http://example.com/a/b/c','../d/e/f') 会变成http://example.com/a/d/e/f../ 只是“一个目录”,afaik ..
      • 因为 c 不是目录(没有最后的斜杠)。目录是 b。一个目录然后是 a/
      • jsfiddle.net/6WycA/7 再次更新.. 没有考虑到,c 将是文件名,而 c/ 会在我的数组中留下一个空格......
      猜你喜欢
      • 1970-01-01
      • 2011-08-14
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 2018-08-14
      • 1970-01-01
      相关资源
      最近更新 更多