我的代码是这样的:
<!DOCTYPE html><html><head> <title>Demo</title> <meta charset="utf-8"/> <script type="text/javascript"> window.onload=function(){ var data='<html>\ <head>\ <meta charset="utf-8">\ <title>Demo</title>\ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"><\/script>\ <script type="text/javascript">\ $(function(){\ alert("abc");\ });\ <\/script>\ <\/head>\ <body>\ </body>\ </html>'; window.frames["test"].document.open(); window.frames["test"].document.write(data); window.frames["test"].document.close(); } </script></head><body> <iframe id="test" frameborder="0" name="test"></iframe></body></html>
这段代码在Chrome、FireFox下均可以正常运行,但在IE下第一次运行会提示$未定义,但刷新后有时又可以正常弹出"abc",所以猜测是运行到alert("abc")这段代码时IE还没有加载完jquery代码。
向大家请教解决方案,通过修改:
$(function(){\ alert("abc");\});\
这一部分,让代码在IE789上可以正常运行,谢谢!
4 回答

精慕HU
TA贡献1845条经验 获得超8个赞
这里提供一种方法,但在FireFox下好像有些问题,会弹出两次:
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta charset="utf-8"/>
<script type="text/javascript">
window.onload=function(){
var data='<html>'+
' <head>'+
' <meta charset="utf-8">'+
' <title>Demo</title>'+
' <script type="text/javascript">'+
' function load_script(url, callback) { '+
' var script = document.createElement("script"); '+
' script.type = "text/javascript"; '+
' if (script.readyState) { '+
' script.onreadystatechange = function() { '+
' if (script.readyState == "loaded" ||script.readyState == "complete") { '+
' script.onreadystatechange = null; '+
' callback(); '+
' } '+
' } '+
' } else { '+
' script.onload = function() { '+
' callback(); '+
' } '+
' } '+
' script.src = url; '+
' document.getElementsByTagName("head")[0].appendChild(script); '+
' }'+
' load_script("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js",function(){'+
' $(function(){'+
' alert("abc");'+
' });'+
' });'+
' <\/script>'+
' <\/head>'+
'<body>'+
'</body>'+
'</html>';
window.frames["test"].document.open();
window.frames["test"].document.write(data);
window.frames["test"].document.close();
}
</script>
</head>
<body>
<iframe id="test" frameborder="0" name="test"></iframe>
</body>
</html>
添加回答
举报
0/150
提交
取消