jQuery外链新窗口打开

Web前端来源:前端开发博客23.9K阅读模式

jQuery外链新窗口打开

对于外链,为了留住用户在本站,我们通常会使用新窗口打开,你可以设置target="_blank"。然而手动一个是麻烦,另一个则是有可能会遗漏,本文通过jQuery查询要点击的链接,添加相关属性,使其打开新窗口。

  1. $(document).on('click','a',function(){
  2.     var url = this.href,hash='';
  3.     if(url.indexOf('#')>-1){
  4.         hash = !!url.split('#')[1] ? '#' + url.split('#')[1] :'';
  5.         url = url.split('#')[0];
  6.     }
  7.     if(url && url.substr(0,4) == 'http' && url.indexOf('caibaojian.com')<0 ) {
  8.         this.href = url.indexOf('?') > -1 ? url+'&utm_source=caibaojian.com'+hash:url+'?utm_source=caibaojian.com'+hash;
  9.         this.setAttribute('target', '_blank');
  10.     }
  11. })

以上代码中,你可以将caibaojian.com换为你自己的域名。

原文:http://caibaojian.com/jquery-open-new-window.html

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
  • JQuery
  • Web前端
评论  2  访客  2
    • 葱白的天蓝
      葱白的天蓝 0

      感觉非常不错,自己现在也在学习相关的知识,有需要的可以加qun436476289大家一起学习交流

      • ctoms
        ctoms 0

        为什么没效果。。 :cry:

      匿名

      发表评论

      匿名网友
      :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

      拖动滑块以完成验证