-->

How to redirect page and js using javascript on ex

2019-09-06 10:11发布

问题:

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