2011-11-08 49 views

回答

2

首先,獲得一個鏈接的絕對URL很簡單:

console.log(doc.links[0].href); 

鏈路的href財產(href屬性)總是會自動讓你得到一個絕對的URL解析沒有任何努力。

我懷疑你真正想要的是解決你從某個地方得到的相對URL。您可以使用nsIIOService此:

var ioService = Components.classes["@mozilla.org/network/io-service;1"] 
          .getService(Components.interfaces.nsIIOService); 
var baseURI = ioService.newURI("http://example.com/index.html", null, null); 
var absURI = ioService.newURI("/test.gif", null, baseURI); 
console.log(absURI.spec); 

這個例子給你http://example.com/test.gif的,所以相對URL /test.gif已經相對於頁面地址http://example.com/index.html解決。

+0

完美,謝謝! – NoBugs