Is there a way to change context to iframe in javascript console? Is there a way to change context to iframe in javascript console? javascript javascript

Is there a way to change context to iframe in javascript console?


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:

enter image description here

Firefox has a similar feature currently in development:

enter image description here


You can also navigate across frames using the command line:

var frame = document.getElementById("frame1").contentWindow;cd(frame);


You can execute code in <iframe>s by using the window.frames[x] functionality. For example,

window.frames[0].runFunction()


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.

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:

enter image description here