将javascript值传入iframe标记 [英] passing in javascript values into iframe tag

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

问题描述

将javascript变量中保存的值传递给同一html页面上的iframe调用的最佳方法是什么?我试图通过移动广告投放JavaScript代码来改善网站的页面响应时间(典型的 document.write('< script type =text / javascriptsrc =..)到一个单独的iframe中。(基于此发布



对广告服务器的请求通常需要一个种子变量,每个站点声明一次,每当客户端加载页面时增加。我想要做的就是将种子变量传入我的iframe部分调用的文档中。

seed变量在我的main的'head'标记中初始化html文档:

 < head> 
< script type =text / javascript>
<! -
custom_seed = 1;
// - >
< / script>
< / head>

后来在html文档中,我通过ifra我返回调用广告服务器所需的html。

 < body> 
<! - 一串html显示页面 - >
< iframe src =somepage.htmlwidth =100%height =100%>
< p>不支持iframe< / p>
< / iframe>
< / body>

'somepage.html'中返回的HTML有一个脚本用于调用广告服务器并需要使用之前声明的种子变量作为参数:

 < script type =text / javascript> 
document.write('< script type =text / javascriptsrc =http://ad.server.net/...seed='+ custom_seed +'?>< / script> ;');
custom_seed ++;
< / script>

实现这个目标的好方法是什么?


主文档:



pre> document.getElementById('IframeID')。src =somepage.html?seed =+ custom_seed;

内部文件:

  var seed = window.location.search.substring(window.location.search.indexOf('seed =')+ 5); 
if(seed.indexOf('&')> = 0){
seed = seed.substring(0,seed.indexOf('&'));
}


What's the best way to pass in the value held in a javascript variable into an iframe call on the same html page? I'm trying to improve my site's page response times by moving ad serving javascript code (the typical document.write('<script type="text/javascript" src="..") into a separate iframe. (Based on this posting)

The request to the ad server typically require a seed variable declared once per site and incremented each time page is loaded by the client. What I want to do is pass in the seed variable into the document invoked by my iframe section.

The seed variable is initialized in the 'head' tag of my main html document:

<head>
    <script type="text/javascript">
    <!--
    custom_seed=1;  
    //-->
    </script>
</head>

Later in the html document, I make the request through an iframe which returns the html necessary to invoke the ad server.

<body>
<!-- a bunch of html to display the page -->
<iframe src="somepage.html" width="100%" height="100%">
<p>No support for iframe</p>
</iframe>
</body>

The html returned in the 'somepage.html' has a script used to call the ad server and needs to use the earlier declared seed variable as a parameter:

<script type="text/javascript">
    document.write('<script type="text/javascript" src="http://ad.server.net/...seed='+ custom_seed  +'?"></script>');
    custom_seed++;
    </script>

What's a good way to achieve this?

解决方案

The only way you can pass values directly to an iframe of a page with a different domain is through the url, as querystring values, anchors, or perhaps some form of rewriting. Even if it's on the same domain, that's probably the easiest and safest way to pass it.

Main document:

document.getElementById('IframeID').src = "somepage.html?seed=" + custom_seed;

Inner document:

var seed = window.location.search.substring(window.location.search.indexOf('seed=') + 5);
if (seed.indexOf('&') >= 0) {
    seed = seed.substring(0, seed.indexOf('&'));
}

这篇关于将javascript值传入iframe标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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