iframe网址更改时发出提醒 [英] Alert when iframe url changed
本文介绍了iframe网址更改时发出提醒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此脚本每2秒运行一次,并检测iframe的url是否已更改。但我无法让它发挥作用。我想提醒iframe的网址是否已更改。例如,如果有人点击链接并在iframed内容上转到另一个页面。
This script runs every 2 seconds and detect if the url of the iframe is changed. But I can't get it to work. I want to alert if the url of the iframe is changed. For example if someone clicks a link and go another page on the iframed content.
var prevSrc = "http://www.bodrumlife.com";
function check() {
var curSrc = $('#iframe').attr('src');
if (curSrc != prevSrc) {
alert("hobaa");
prevSrc = curSrc;
}
}
window.setInterval(check, 2000); // 2 seconds
<iframe id="iframe" name="iframe" src="http://www.bodrumlife.com"></iframe>
推荐答案
如果您更喜欢使用JQuery,请使用以下语法,而不是使用half-jquery和标准javascript。
If you prefer to use JQuery below is the following syntax, instead of using half-jquery and standard javascript.
$(function(){
$('#iframeId').load(function() {
alert("the iframe has changed.");
});
});
虽然我不确定onload函数是否适用于Sarafi。它受到Firfox,IE和Chrome的支持。
Although I'm not sure if the onload function works in Sarafi. It's supported by Firfox, IE and Chrome.
编辑:
扩展示例
<script type="text/javascript">
$(function () {
var intialFrameSrc = "http://www.twiij.com";
$("#iframeContentPanel").load(function () {
if (intialFrameSrc != $("#iframeContentPanel").attr('src')) {
alert("the iframe has changed.");
}
});
$("#btnChangeUrl").click(function () {
$("#iframeContentPanel").attr("src", "http://m.smh.com.au/");
});
});
</script>
<iframe id="iframeContentPanel" name="iframeContentPanel" src="http://www.twiij.com" width="500" frameborder="0" height="500" scrolling="no"></iframe>
<input type="button" id="btnChangeUrl" value="Change Url"/>
这篇关于iframe网址更改时发出提醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文