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.
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:
Firefox has a similar feature currently in development:
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,
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:
For firebug solution see this answer on another SO question. Doesn’t work cross-domain like Dennis’s Chrome solution however.
Edit: With newer versions of firebug they may have fixed cross-domain issue.
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.
Calling cd() without parameters returns to the top-level window.
Allows you to change command-line expression evaluation from the default top-level window of the webpage to the window of a frame.
More info, here