2014-07-03 121 views

回答

4

一個簡單的方法是創建一個鏈接元素,設置其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是絕對的。

請注意,我們在此處使用反射屬性而不是屬性很重要。

+0

這實在太神奇了!從來沒有想過它!真棒!非常感謝你! – Roy

+0

@rmrinmoy :-)不用擔心。如果這個*回答了你的問題,Stack Overflow的工作方式,你可以通過點擊它旁邊的複選標記來「接受」答案。但是隻有當它確實回答了這個問題。 –