How to redirect page and js using javascript on external domain based on referer?

∥☆過路亽.° 提交于 2019-12-13 05:34:30

问题


I have some sites mysite1.com, mysite2.com,....mysiten.com

All mysite1-n.com using external javascript on myexternalsite.com/mainfile.js

I want,

if (mysite1-n.com) visitors come from www.google.com then will redirect to welcome.com

if (mysite1-n.com) visitors come from www.yahoo.com then will redirect to welcome2.com

if (mysite1-n.com) visitors come from www.anotherdomain.com then will call javascript on myexternalsite.com/file1.js and working using this script

And if (mysite1-n.com) visitors come from bookmark then will call javascript on myexternalsite.com/file2.js and working using this script

What sort of script should I be using on myexternalsite.com/mainfile.js ?

Thank's


回答1:


basically you should check for document.referrer value and the document.location.href to do achieve what you want. This with a bunch of regexp should do the trick easily.

ex:

if( document.location.href.match(/^https?:\/\/mysite1-n.com/) ){
  if( document.referrer.match(/google.com/) ){
    window.location = 'http://welcome.com';
  }
  var s = document.createElement('script');
  s.src = 'myexternalsite.com/mainfile.js';
  document.head.appendChild(s);
}

and so on



来源:https://stackoverflow.com/questions/10581503/how-to-redirect-page-and-js-using-javascript-on-external-domain-based-on-referer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!