weixin_33734785 2016-03-21 14:13 采纳率: 0%
浏览 26

在IE上失败测试Same Origin

This test (to sending a cross domain AJAX or not) is always false on Internet Explorer but it works on Microsoft Edge.

It looks like, the <a> element doesn't populate on IE.

function testSameOrigin(url){
    /*
        Return true if belongs to the same origin
    */
    var loc = window.location,
        a = document.createElement('a');

    a.href = url;

    return a.hostname == loc.hostname &&
           a.port == loc.port &&
           a.protocol == loc.protocol;
}

How can I fix this?

Thanks for helping.

  • 写回答

1条回答 默认 最新

  • perhaps? 2016-03-23 08:37
    关注

    I fix it :

    function testSameOrigin(url){
    
            var canonicalize = function(url) {
                var div = document.createElement('div');
                div.innerHTML = "<a></a>";
                div.firstChild.href = url;
                div.innerHTML = div.innerHTML;
                return div.firstChild.href;
            };
    
        var loc = window.location
          , a = document.createElement('a');
    
        a.href = canonicalize(url);
    
        return a.hostname == loc.hostname &&
               ( a.port == loc.port || ((a.port == 80 || a.port == 443 ) && loc.port =="") ) &&
               a.protocol == loc.protocol;
    }
    

    This post help me to find my way.

    评论

报告相同问题?