Fastest way to detect external URLs

前端 未结 9 1445
孤街浪徒
孤街浪徒 2020-12-02 14:23

What\'s the fastest method to detect if foo=\'http://john.doe\' is an external url (in comparsion to window.location.href)?

9条回答
  •  星月不相逢
    2020-12-02 15:16

    pseudosavant's answer didn't exactly work for me, so I improved it.

    var isExternal = function(url) {
        return !(location.href.replace("http://", "").replace("https://", "").split("/")[0] === url.replace("http://", "").replace("https://", "").split("/")[0]);   
    }
    

提交回复
热议问题