Javascript 有没有办法在javascript控制台中将上下文更改为iframe?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7961229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to change context to iframe in javascript console?
提问by Muhd
I would like to change the context of the javascript executed in the webkit developer tool/firebug console to execute its code like it is running from inside an iframe on the page.
我想更改在 webkit 开发人员工具/firebug 控制台中执行的 javascript 的上下文,以执行其代码,就像它从页面上的 iframe 内部运行一样。
I know I could do this by opening the page in the iframe on a separate page, but I want to run code where it interacts with the parent frame.
我知道我可以通过在单独页面上的 iframe 中打开页面来做到这一点,但我想在它与父框架交互的地方运行代码。
回答by Dennis
Chrome 15 allows you to change the scope of the console. On the bottom of the console, next to the clear console button, there is a menu that says <top frame>
which will give a list of available frames:
Chrome 15 允许您更改控制台的范围。在控制台底部,在清除控制台按钮旁边,有一个菜单,<top frame>
其中显示了可用框架的列表:
Firefox has a similar featurecurrently in development:
Firefox目前正在开发一个类似的功能:
You can also navigate across frames using the command line:
您还可以使用命令行跨帧导航:
var frame = document.getElementById("frame1").contentWindow;
cd(frame);
回答by LoveAndCoding
You can execute code in <iframe>
s by using the window.frames[x]
functionality. For example,
您可以<iframe>
使用该window.frames[x]
功能在s 中执行代码。例如,
window.frames[0].runFunction()
回答by dpercy
In today's Chrome (version 52), all you have to do is select the iframe in the "Elements" tab of the dev tools. Anything you run in the JS console will automatically run in the context of the selected iframe.
在今天的 Chrome(版本 52)中,您所要做的就是在开发工具的“元素”选项卡中选择 iframe。您在 JS 控制台中运行的任何内容都将自动在所选 iframe 的上下文中运行。
For example, here I've selected an iframe, and when I type document.location.pathname
into the console it returns the src attribute of the iframe, instead of the URL from the address bar:
例如,这里我选择了一个 iframe,当我document.location.pathname
在控制台中输入时,它返回 iframe 的 src 属性,而不是地址栏中的 URL:
回答by Muhd
For firebug solution see this answeron another SO question. Doesn't work cross-domain like Dennis's Chrome solution however.
对于萤火虫解决方案,请参阅另一个 SO 问题的答案。但是,不能像 Dennis 的 Chrome 解决方案那样跨域工作。
Edit: With newer versions of firebug they may have fixed cross-domain issue.
编辑:使用较新版本的萤火虫,他们可能已经修复了跨域问题。
回答by Sebastian
Execution of script statements and commands by default is done in context of the top-level window. If you are using frames, use the "cd()" console command.
默认情况下,脚本语句和命令的执行是在顶级窗口的上下文中完成的。如果您使用框架,请使用“cd()”控制台命令。
cd()Calling cd() without parameters returns to the top-level window.
cd()不带参数调用 cd() 返回顶层窗口。
cd(window)Allows you to change command-line expression evaluation from the default top-level window of the webpage to the window of a frame.
cd(window)允许您将命令行表达式评估从网页的默认顶级窗口更改为框架窗口。
More info, here
更多信息,在这里
回答by Andreyka Bonart
cd(document.getElementsByTagName('iframe')[0]);