我的页面中有以下代码
<html>
<head>
<title>testpage</title>
<script language = 'javascript'>function fchange(){alert(document.getElementById("ifrm").value);</script>
</head>
<body>
<iframe id = 'ifrm' src = 'http://www.google.com' width = '700' height='500'></iframe><input type='button' onclick = 'fchange()' value = 'clickhere'>
</body>
</html>
我点击按钮,警告框显示未定义
。但我需要页面的内容或来源('http://www.google.com')。请帮我做到这一点。
提前致谢...
最佳答案
你不能这样做,因为它破坏了 same-origin policy .
如果两个页面都在同一个域中,那么您可以按照 @Joel 的建议进行操作,或者稍微老式一点:
window.frames['ifrm'].document.body.innerHTML;
您需要<iframe name="ifrm" ...>
为此。
关于javascript - 获取页面源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/666290/