捕获传入的 URL 参数,然后使用 Javascript 传递给 iFrame Src

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10435512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:18:23  来源:igfitidea点击:

Capture incoming URL Parameters, then pass to iFrame Src with Javascript

javascriptiframegoogle-analyticsquery-stringwindow.location

提问by jgrannis

So trying to figure out how to do this with window.location in Javascript. I'm sending users to our site with an appended URL with a Google Analytics code that I need to pass to an iframe src on that page. I'd assume Javascript could do this (note - I cannot use PHP)...

所以试图弄清楚如何在 Javascript 中使用 window.location 来做到这一点。我将用户发送到我们的网站,并附加了一个带有 Google Analytics 代码的 URL,我需要将其传递给该页面上的 iframe src。我假设 Javascript 可以做到这一点(注意 - 我不能使用 PHP)...

This is what I want to do:

这就是我想要做的:

I'd send users to the page with all the campaign data in tact. For example a user would click on this link: http://www.xyz.com/index.html?utm_source=Facebook&utm_campaign=Facebook+May&utm_medium=click

我会将用户发送到包含所有活动数据的页面。例如,用户会单击此链接:http: //www.xyz.com/index.html?utm_source=Facebook& utm_campaign=Facebook+May& utm_medium=click

They would be directed to that page, that then has this iFrame on it. The code on the store side would need to pick up utm_source, utm_campaign, utm_medium and include these parts in the IFRAME SRC So this bit:

他们会被定向到那个页面,然后在上面有这个 iFrame。商店端的代码需要获取 utm_source、utm_campaign、utm_medium 并将这些部分包含在 IFRAME SRC 中 所以这一点:

<iframe height="960px" frameborder="0" scrolling="no" width="958px" src="http://www.abc.com/minis"></iframe>

now becomes:

现在变成:

 <iframe height="960px" frameborder="0" scrolling="no" width="958px" src="http://www.abc.com/minis?utm_source=Facebook&utm_campaign=Facebook+May&utm_medium=click"></iframe>

Any javascript suggestions would be greatly appreciated. Note - I cannot use PHP.

任何 javascript 建议将不胜感激。注意 - 我不能使用 PHP。

UPDATE: Got this to work!! Yay, but now I need to edit it a bit:

更新:让这个工作!是的,但现在我需要稍微编辑一下:

So say the appended url that was clciked was this:

所以说被点击的附加网址是这样的:

http://abc.com/index.html?apple&orange&peach

and I need the iframe src to be this

我需要 iframe src 是这个

 http://xyz.com/minis?orange&peach

I moved a few things around in the script, but is now only grabbing orange and not the other & attribute (peach). please advise if there is a better way to work (without have all the params and then depending on what link comes in, some of the & will be undefined:

我在脚本中移动了一些东西,但现在只抓取橙色而不是其他 & 属性(桃子)。请告知是否有更好的工作方式(没有所有参数,然后根据进入的链接,一些 & 将是未定义的:

<body>

<script type="text/javascript">

$(function() {

var loc = window.location.toString(),
params = loc.split('&')[1],
params2 = loc.split('&')[2],
params3 = loc.split('&')[3],
params4 = loc.split('&')[4],
params5 = loc.split('&')[5],
params6 = loc.split('&')[6],
  iframe = document.getElementById('myIframe');
alert(iframe.src); 
iframe.src = iframe.src + '?' + params + '&' + params2 + '&' + params3 + '&' + params4+ '&' + params5;
alert(iframe.src); 

});
</script>
<iframe id="myIframe" src="http://www.xyz.com/minis"></iframe>

</body>

采纳答案by GillesC

This little snippet should do, here all you have to do is grab the bit after ? as a string and append it to the iframe source.

这个小片段应该做,在这里你所要做的就是抓住 ? 作为字符串并将其附加到 iframe 源。

var loc = window.location.toString(),
    params = loc.split('?')[1],
    iframe = document.getElementById('myIframe');

iframe.src = iframe.src + '?' + params;????????????????