我需要获取当前网页'地址(URL)。 我是这样做的:
var PageUrl = window.location.href;
console.log('Current URL');
console.log(PageUrl);
例如,在http://www.infoq.com/research/dotnet-testing-tools中,我想获取此页面的网址,但我收到了很多网址:
Current URL
http://tpc.googlesyndication.com/safeframe/1-0-2/html/container.html
Current URL
http://static.ak.facebook.com/connect/xd_arbiter/PqAPbTuc2cR.js?version=41#channel=f35bc2aebc&origin=http%3A%2F%2Fwww.infoq.com
Current URL
https://s-static.ak.facebook.com/connect/xd_arbiter/PqAPbTuc2cR.js?version=41#channel=f35bc2aebc&origin=http%3A%2F%2Fwww.infoq.com
Current URL
http://www.infoq.com/research/dotnet-testing-tools
Current URL
https://www.facebook.com/connect/ping?client_id=273443186033957&domain=www.…c%26relation%3Dparent&response_type=token%2Csigned_request%2Ccode&sdk=joey
Current URL
http://s7.addthis.com/static/sh.99e50185.html#rand=0.853022777242586&iit=14…efab5561013%26chr%3DUTF-8%26md%3D0%26vcl%3D1&rev=v2.4.4-wp&ct=1&xld=1&xd=1
我只需要当前的网页' url(即,http://www.infoq.com/research/dotnet-testing-tools),为什么有很多网址?我怎样才能得到唯一一个?
答案 0 :(得分:0)
请尝试使用document.location.href
。
var PageUrl = document.location.href;
console.log('Current URL');
console.log(PageUrl);
原因是window
不仅拾取了该文档中的所有位置,还拾取了该文档中帧内指定的页面。