admin管理员组文章数量:1332345
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 = "";
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=""></iframe>
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.";
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."></iframe>
Share
Improve this question
asked Feb 21, 2012 at 4:55
user198989user198989
4,66320 gold badges68 silver badges95 bronze badges
1
- 4 stackoverflow./questions/2429045/… – Alex Commented Feb 21, 2012 at 4:58
1 Answer
Reset to default 4If 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.");
});
});
Although I'm not sure if the onload function works in Sarafi. It's supported by Firfox, IE and Chrome.
EDIT:
extended example
<script type="text/javascript">
$(function () {
var intialFrameSrc = "http://www.twiij.";
$("#iframeContentPanel").load(function () {
if (intialFrameSrc != $("#iframeContentPanel").attr('src')) {
alert("the iframe has changed.");
}
});
$("#btnChangeUrl").click(function () {
$("#iframeContentPanel").attr("src", "http://m.smh..au/");
});
});
</script>
<iframe id="iframeContentPanel" name="iframeContentPanel" src="http://www.twiij." width="500" frameborder="0" height="500" scrolling="no"></iframe>
<input type="button" id="btnChangeUrl" value="Change Url"/>
本文标签: javascriptAlert when iframe url changedStack Overflow
版权声明:本文标题:javascript - Alert when iframe url changed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742325770a2453693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论