如何检查框架源是否已更改并在新窗口中开源
我们正在创建一个基于网络的移动应用程序。该应用程序有一个在窄框架中加载的广告。在 iPhone 上进行测试时,我们注意到,当点击广告时,它会在(本机应用程序的)广告框架内加载,而不是在新窗口中打开。
我们现在希望通过检查框架的来源来检测广告是否被点击。如果源已更改,我们希望获取该 URL 并使用 Javascript 在新窗口中打开它。然后,我们想将帧的来源重置为原始。
这是我们目前为止的代码。
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js'></script>
<script>
$(document).ready(function() {
setTimeout( checkAdLocation, 250 );
});
function checkAdLocation() {
var adLocation = window.frames['adframe'].document.location;
if(adLocation!='http://path/play.php?showad') {
window.frames['adframe'].document.location.href = 'play.php?showad';
window.open( adLocation );
}
alert( adLocation);
setTimeout( checkAdLocation, 2000 );
}
</script>
<frameset cols='50%,50%'>
<frame src='play.php?showad' name=adframe>
<frame src='http://google.com'>
</frameset>
谢谢。
We are creating a web-based mobile app. The app has an ad that loads in a narrow frame. In testing on the iPhone, we noticed that when the ad is clicked, it loads within the ad frame (of the native app), instead of opening in a new window.
We are now looking to detect whether an ad has been clicked by checking the source of the frame. If the source has changed, we would like to grab that URL and open it in a new window using Javascript. Then, we would like to reset the source of the frame to the original.
Here is the code we have so far.
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js'></script>
<script>
$(document).ready(function() {
setTimeout( checkAdLocation, 250 );
});
function checkAdLocation() {
var adLocation = window.frames['adframe'].document.location;
if(adLocation!='http://path/play.php?showad') {
window.frames['adframe'].document.location.href = 'play.php?showad';
window.open( adLocation );
}
alert( adLocation);
setTimeout( checkAdLocation, 2000 );
}
</script>
<frameset cols='50%,50%'>
<frame src='play.php?showad' name=adframe>
<frame src='http://google.com'>
</frameset>
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的代码来看,广告似乎位于同一域中?如果是这种情况,最简单的方法可能是向该帧中的所有 a 标签注入
target="_blank"
。使用 jQuery(您在示例中包含了它,所以我假设可以使用),这应该可以做到:
From your code, it looks like the ad lives on the same domain? If that is the case, it's probably easiest to just inject a
target="_blank"
to all a tags in that frame.Using jQuery (which you included in your example so I'm assuming that is okay to use), this should do it: