2017-05-03 67 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應在整個頁面中使用,而不是在一個框架。如果您想在框架上使用警報,請改爲使用模式。