How to check if frame source has changed and open source in new window
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() 开发者_JAVA技巧{
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.
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:
$(adframe.document.body).find('a').attr('target', '_blank');
精彩评论