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

时间:2021-07-25 00:26:58

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)...

我想知道怎么用window。位置在Javascript。我用一个带有谷歌分析代码的附加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

我将用tact中的所有活动数据将用户发送到页面。例如,用户可以点击这个链接:http://www.xyz.com/index.html?

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:

假设被clciked的附加url是:

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:

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

<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>

1 个解决方案

#1


12  

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;​​​​​​​​​​​​​​​​

#1


12  

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;​​​​​​​​​​​​​​​​