将网址参数传递给iframe [英] Passing URL parameters to iframe

查看:128
本文介绍了将网址参数传递给iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从包含 iframe 的页面获取参数,并将参数附加到 src iframe



示例:

含页面的IFRAME -



http://www.example.com/pref.html?c=%433r3jutlut9se%23&e=test%40example.com



上一页的IFRAME -

 < iframe id =myiframename =myiframesrc =prefcontent.aspxframeborder =0
width =100%height =800>< / iframe>

下面是我尝试过的很多东西之一,我认为它理论上可行,但我没有运气:

 < script language =javascript> 
$(function(){

var loc = window.location.toString(),
params = loc.split('?')[1],
params2 = loc.split('&')[2],
iframe = $('#myiframe');

console.log(params);

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


});
< / script>


解决方案

如果您使用jQuery,您可能想要剥离您的代码:

  $(function(){
var search = window.location.search;
$(#myiframe)。attr(src,$(#myiframe)。attr(src)+ search);
});

使用window.location.search;因为它会在url结尾删除哈希标记,因为它是本地代码,所以速度要快很多。

然后使用jQuery选择器来查找和替换属性src在iframe上,它的当前src加上从这个页面获得的搜索。



希望这有帮助。


I would like to grab the parameters from a page that contains an iframe, and append the parameters to the src of the iframe.

Example:

PAGE CONTAINING IFRAME--

http://www.example.com/pref.html?c=%433r3jutlut9se%23&e=test%40example.com

IFRAME IN THE ABOVE PAGE--

<iframe id="myiframe" name="myiframe" src="prefcontent.aspx" frameborder="0"
width="100%" height="800"></iframe>

Here's one of the many things I've tried, which I thought would work in theory, but I had no luck with it:

<script language="javascript">
$(function() {

    var loc = window.location.toString(),
    params = loc.split('?')[1],
    params2 = loc.split('&')[2],
    iframe = $('#myiframe');

    console.log(params);

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


});
</script>

解决方案

if your using jQuery you might want to strip down your code up:

$(function() {
    var search = window.location.search;
    $("#myiframe").attr("src", $("#myiframe").attr("src")+search);
});

Use window.location.search; as it will snip off the hash tag at the end of the url and is a lot faster as it is native code.

Then use jQuery selectors to find and replace the attribute of src on the iframe, with it's current src plus the search acquired from this page.

Hope this helps.

这篇关于将网址参数传递给iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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