Using jQuery to check if a link is internal or external

后端 未结 8 932
南旧
南旧 2020-12-29 09:29

I want to write a script which can determine whether a link is internal or external. This is simple from my perspective, all internal links are relative, so they start with

8条回答
  •  悲哀的现实
    2020-12-29 10:08

    $(document).ready( function() {
    $('a[href^="http"]').not('a[href^="http://' + $(location).attr('hostname') + 
    '"]').attr('target', '_blank');
    });
    

    Replace "http" with "https" if you need to

提交回复
热议问题