javascript - 如何在 iframe 上运行 javascript 代码?

标签 javascript iframe

我一直在尝试在index.php文件中的iframe上运行javascript代码,但没有取得任何成功,所以如果你们可以请看一下我做错了什么:

这是我的代码:

<script type="text/javascript">
window.onload = function() {
document.getElementById("resultFrame").contentWindow.iframe();
function iframe() {
var isCandidateRegion=function(node){
return (node.innerText.indexOf('Username')>-1 && node.innerText.indexOf('Hours')>-1);
};
//Find the last table in th document that contains 'Username' and 'Hours'
var candidateRegions=[].filter.call(document.querySelectorAll('table'),isCandidateRegion);
var targetRegion=candidateRegions[candidateRegions.length-1];
var isVisible=function(node){
return (node.offsetWidth && node.offSetWidth >0) || (node.offsetHeight && node.offsetHeight>0);
};
var inputs=[].filter.call(targetRegion.querySelectorAll('input'),isVisible);
inputs[0].value="nicefellow1234";
inputs[1].value="23";
inputs[2].click();
};
};
</script>

<iframe id="resultFrame" src="http://www.infamousgangsters.com/site.php?page=kill" height="700px" width="100%"></iframe>

此外,我在控制台中收到如下错误:

Error: Permission denied to access property 'iframe'

所以如果你们可以帮助我......!

最佳答案

我认为它被拒绝是因为该域与托管 iframe 的域不同。这是为了保护用户免受 XSS 攻击。

关于javascript - 如何在 iframe 上运行 javascript 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27748329/

相关文章:

javascript - onclick 当ajax响应到达时准备html并在react中渲染?

javascript - 图形被切断 : jQuery Image Slider

.htaccess - 如何将这个异常添加到htaccess中?

google-chrome - 允许 Youtube iframe 显示与 Youtube.com 兼容的 Chrome 扩展程序

javascript - 防止用户从嵌入的 YouTube 视频获取 url/禁用剪贴板

facebook - 创建 Canvas 后没有 'Add to my Page' 的菜单?

javascript - 使用 vanilla js 和 css 的动画移动动画

javascript - promise 后的返回值

javascript - React hooks 状态不使用最新版本

html - 设置 &lt;iframe&gt; 的 HTTP 请求类型