<html>
<head>
<link rel="stylesheet" href="my.css" />
</head>
<body>
<iframe id="baidu-container" src="http://image.baidu.com/i?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1374487244324_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=giant" frameborder="0">
</iframe>
<script type="text/javascript" src="my.js"></script>
</body>
</html>
我想在iframe完全加载后在my.js
中执行脚本。那我该怎么办?感谢。
答案 0 :(得分:1)
您可以在iframe上使用onload事件:
答案 1 :(得分:0)
<html>
<head>
<link rel="stylesheet" href="my.css" />
</head>
<body>
<iframe id="baidu-container" src="http://image.baidu.com/i?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1374487244324_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=giant" frameborder="0">
</iframe>
<script>
window.document.getElementById("baidu-container").onload = function() {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "my.js";
window.document.head.append(s);
}
</script>
</body>
</html>
load
事件。答案 2 :(得分:0)
回答标题中的问题。如果iframe中的页面来自其他域,则不能。否则:
function addScript() {
var iframeDoc = iframe.contentDocument;
var s = iframeDoc.createElement('script');
s.type = 'text/javascript';
s.src = 'my.js';
iframeDoc.body.appendChild(s);
}
// I'm creating the iframe programmatically, to make sure the onload handler fires.
var iframe = document.createElement('iframe');
iframe.width = 300;
iframe.height = 100;
iframe.onload = addScript;
iframe.src = "iframe-test2.html";
document.body.appendChild(iframe);