如何在外部网站上基于引荐来源使用javascript重定向页面和js? [英] How to redirect page and js using javascript on external domain based on referer?

查看:107
本文介绍了如何在外部网站上基于引荐来源使用javascript重定向页面和js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些站点mysite1.commysite2.com,.... mysiten.com

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

所有mysite1-n.commyexternalsite.com/mainfile.js

我想要

如果(mysite1-n.com)位访问者来自www.google.com,则将重定向到welcome.com

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

如果(mysite1-n.com)位访问者来自www.yahoo.com,则将重定向到welcome2.com

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

如果(mysite1-n.com)位访问者来自www.anotherdomain.com,则将在myexternalsite.com/file1.js上调用javascript并使用此脚本进行工作

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

如果(mysite1-n.com)位访问者来自书签,则将在myexternalsite.com/file2.js上调用javascript并使用此脚本进行工作

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

我应该在myexternalsite.com/mainfile.js上使用哪种脚本?

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

谢谢

推荐答案

基本上,您应该检查document.referrer值和document.location.href来实现所需的功能.带有一堆正则表达式可以轻松解决问题.

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.

例如:

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);
}

以此类推

这篇关于如何在外部网站上基于引荐来源使用javascript重定向页面和js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆