为什么document.URL没有显示当前的url?

时间:2014-02-28 07:12:01

标签: jquery

请你告诉我 为什么document.URL没有显示当前的url 正确的网址:http://jsfiddle.net/PWEAR/20/ 显示:http://fiddle.jshell.net/_display/

这是我的小提琴: http://jsfiddle.net/PWEAR/20/

 console.log(document.URL + ': ' + CheckUrl(document.URL));
    alert("http://ca.firstrail.com: " + CheckUrl("http://ca.firstrail.com"));
    alert("http://fr.firstrail.com: " + CheckUrl("http://fr.firstrail.com"));

    function CheckUrl(url) {
        var rx = new RegExp('http://(ca|fr)');
        var result = rx.exec(url);
        return result && result[1] || 'no fr or ca';
    }

3 个答案:

答案 0 :(得分:1)

这是因为jsFiddle为窗口使用iframe,因此 显示该帧的正确URL。

在jsFiddle之外尝试它应该有效,并且记住你永远不应该设置document.URL,这是window.location的用途

答案 1 :(得分:0)

我认为这是因为jsFiddle的机制。该脚本未嵌入页面内,它位于框架内。

如果你正在使用chrome,你应该可以自己检查一下:

enter image description here

答案 2 :(得分:0)

尝试window.location.href而不是document.URL

似乎Fiddle使用iframe,document.URL带来iframes网址