2017-05-03 66 views
1

我对JavaScript很陌生,我使用下面的代码输出警告框。我不知道为什么,但警告框似乎并不在该功能内部工作,但会在其外部非常愉快地工作。我将如何使它从这个函数内出现?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>

回答

0

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

当你去掉这一行:

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

它将工作。我看到两个相同的ID iframesA在你的HTML!

尝试更换alert()console.log("iframesA");

,你也可以做的setInterval,之外的功能:

setInterval(yourFunction, 3000); 

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

注:我认为它不显示页面的好方法。 iframe会对https或混合http/https网站产生严重问题...

0

警报在JavaScript应在整个页面中使用,而不是在一个框架。如果您想在框架上使用警报,请改为使用模式。