Javascript统一设置外链在新窗口打开

日期:2011-11-30    阅读:45   分类:Javascript


在写博客的时候,添加的友情链接,引用别的网站的链接,或者是图片等资源,我们希望外链可以在新窗口打开,一个一个的去设置target="_blank"实在太繁琐了,因此就写了一段javascript来完成,对于本站来说,只需要如下脚本即可:

function openLinks(){
   
    var anchors = document.getElementsByTagName("a");
    var anchor = null, rel = "", href = "";
    for (var i=0; i<anchors.length; i++){
        anchor = anchors[i];

        href = anchor.getAttribute("href");
        if(href && href.indexOf("http://") > -1 && href.indexOf("scriptlover.com") == -1)
        {
            anchor.setAttribute("target" , "_blank");
        }

        rel = anchor.getAttribute("rel");
        if (rel == "external" || rel == "nofollow")
        {
            anchor.setAttribute("target" , "_blank");
        }
    }
}

本页链接: http://www.scriptlover.com/static/1248-javascript-链接-窗口

标签:

相关文章

网友评论

Leave a comment

 required

 required (Not published)

 required