如何使用JS将查询字符串参数附加到所有出站URL?

时间:2016-12-23 16:32:15

标签: javascript php iframe local-storage url-redirection

这个问题可能看起来很简单,但我网页上的一些链接嵌入了视频iframe中,而这些iframe并非由我托管。所有出站链接都是相同的,但即使是视频,也有办法强制任何人点击任何链接全部转到同一个网址,仅通过附加初始查询sting来修改它吗?

基本上该页面是sub.mysite.com,其上的所有链接都转到mysite.com - 我需要的是登陆sub.mysite.com?x=landing的任何人都可以访问mysite.com/landing他们点击sub.mysite.com上的任何链接,包括视频链接。这有可能吗?

我想的是:

var urlParams;
(window.onpopstate = function () {
    var match,
        pl     = /\+/g,  // Regex for replacing addition symbol with a space
        search = /([^&=]+)=?([^&]*)/g,
        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
        query  = window.location.search.substring(1);

    urlParams = {};
    while (match = search.exec(query))
       urlParams[decode(match[1])] = decode(match[2]);
})();

然后像:

var anchors = document.getElementsByTagName('a');
for(var i = 0; i < anchors.length; i++) {
  var a = anchors[i];
  if(a.href) {
    a.addEventListener('mousedown', function(e) {
      this.href = 'mysite.com/' + x;
    }, false);
  }
}

但不确定这是否适用于iframe。是否有一些直接强制重定向每个出站链接的方法?也许用一些PHP来帮忙?还是一个htaccess动态规则?

0 个答案:

没有答案