Javascript iframe 中的警告框

Javascript Alert box within iframe

我是 javascript 的新手,我正在使用下面的代码输出警告框。我不确定为什么,但警告框似乎在函数内部不起作用,但在函数外部会非常愉快地工作。我怎样才能让它出现在这个函数中?

我觉得我还应该提到下面的代码是在 iframe 本身中执行的。

感谢任何帮助

$(window).on('beforeunload', function() {
    $(window).scrollTop(0);
});


function yourFunction(){

    alert("Alert");

    document.getElementById('iframeA').src = document.getElementById('iframeA').src

    setTimeout(yourFunction, 3000);

}      


function yourFunctionTwo(){

    document.getElementById('iframeB').src = document.getElementById('iframeB').src


    setTimeout(yourFunctionTwo, 4000);
}


yourFunction();
yourFunctionTwo();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
    
</head>

<iframe id="iframeA" src="" height="50%" width="100%"></iframe>
<iframe id="iframeB" src="/sms/frames/tickerB.php" height="50%" width="100%" style="display: block; position: absolute;"></iframe>
<iframe id="iframeA" src="/sms/frames/tickerA.php" height="50%" width="100%" style="display: block; position: absolute;"></iframe>



</html>

javascript 中的警报应在整个页面中使用,而不仅仅是在框架内使用。如果您想在框架上使用警报,请改用模态。

首先尝试打开您的 javascript 控制台进行调试。

当您取消注释此行时:

document.getElementById('iframeA').src = document.getElementById('iframeA').src

它会起作用的。我在你的 html!

中看到两次相同的 id iframesA

尝试用 console.log("iframesA");

替换 alert()

你也可以在函数外做一个setInterval:

setInterval(yourFunction, 3000);

我做了一个JSBin http://jsbin.com/rixigicugu/edit?html,js,output

注意:我认为这不是显示页面的好方法。 iframe 与 https 或混合 http / https 站点会有很大问题...