Home  >  Article  >  Web Front-end  >  How to solve the slow loading of JS iFrame

How to solve the slow loading of JS iFrame

高洛峰
高洛峰Original
2017-01-12 11:05:412152browse

In projects, it is often necessary to dynamically add iframes, and then perform related operations on the added iframes. Sometimes, why does the iframe load slowly? How to solve it? Let’s study through this article with this question in mind and find the answer!

aaa.html

<HTML>
<HEAD>
<TITLE>aaa</TITLE>
</HEAD>
<BODY>
<IFRAME src="bbb.html" name=bbb width="100%" height="190"> </IFRAME>
<INPUT type="button" value="显示text控件值" onclick="alert(bbb.document.all.txt.value)"> 
<SCRIPT LANGUAGE="JavaScript">
alert(bbb.document.all.txt.value);
</SCRIPT>
</BODY>
</HTML>

bbb.html

<HTML>
<HEAD>
<TITLE>bbb</TITLE>
</HEAD>
<BODY>
<input type=text name=txt value="guoguo"> 
</BODY>
</HTML>

Problem:

Execute the above aaa.html and find that the value of alert is not printed directly in the code. But clicking the button can print out its value.

Analysis:

When the page is loading, if it encounters an iframe, it will jump directly to it, load the following content, and then come back to load the iframe. Of course, it can also be understood as opening another thread when it encounters the iframe. to load the iframe, but because it involves new IO operations that are time-consuming, the loading of the iframe is still later than the execution of the js code at the bottom of the page, so the above problem occurs.

Solution:

Add a delay in the js code (the specific delay time can be determined by personal experience), so that the objects in the iframe can be obtained normally.

<SCRIPT LANGUAGE="JavaScript">
setTimeout("alert(bbb.document.all.txt.value)",1500);
</SCRIPT>

Conclusion: When a page contains an iframe, if we want to operate the objects in the iframe through js, we must wait until the iframe is loaded before operating, otherwise we will not get the desired object.

The above is the solution to the slow loading of JS iFrame introduced by the editor. I hope it will be helpful to you. At the same time, I also thank you very much for your support of the PHP Chinese website!

For more related articles on how to solve the slow loading of JS iFrame, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn