簡體   English   中英

在javascript中使用任意根制作相對url絕對值

[英]making relative url absolute using arbitrary root in javascript

假設我在不同域(mydomain.com)上的頁面上,並且相對URL僅存在於代碼中(不在DOM中)

如何在javascript中完全組合兩個任意網址?

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模塊 ,但我還沒有找到一種在客戶端重用它的好方法。 如何在客戶端使用node.js模塊系統

我設法通過破解我的方式使其工作,但這不是一個真正的解決方案我覺得放入生產網站很舒服。 Hackety hack

我在服務器端使用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/');

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/'));

都給出了預期的結果

無法抗拒解決方案

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

適用於測試用例和兩者的組合

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

這是一種可能但未經測試的解決方案:

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('/');
}

應該照顧兩個案件......

我以為我理解了這個問題但是我的小提琴返回了兩個錯誤。 這些例子並不明顯

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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM