123

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.

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.

Muhd
  • 24,305
  • 22
  • 61
  • 78
  • 1
    you can always execute code in the `window.frames[x]` fashion. Just append whatever command you want. I.E. `window.frames[0].runFunction()` – LoveAndCoding Nov 01 '11 at 00:30
  • 1
    @Ktash, would you perhaps like to make that an answer? – Muhd Nov 01 '11 at 01:18
  • How I can do the same in IE? I find it extremely difficult to select an element in the console window using $ if the element is located in an inner IFRAME. Please help. – tarekahf May 26 '17 at 15:59

7 Answers7

173

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);
Community
  • 1
  • 1
Dennis
  • 32,200
  • 11
  • 64
  • 79
  • 8
    Can I execute code in the console to accomplish this same thing, or do I have to click the frame selector? – bodine Aug 07 '13 at 23:25
  • @bodine - have you found a way to accomplish it from the console command ? – arty Feb 11 '14 at 11:58
  • 1
    Note that this dropdown is now at the top of the console, rather than the bottom. – Muhd May 08 '14 at 18:51
  • 1
    I don't see this in Chrome 38. Couldn't quickly figure out how to do this in Chrome; used the cd(frames[]) in Firefox instead. – Akrikos Oct 30 '14 at 14:51
  • Note in Firefox you can also select an iframe via element picker, then do `cd($0)` instead of pointing to it via ID – jakub.g Nov 25 '14 at 16:04
  • 1
    What a shame. It won't retain the selected frame if the frame's content is changed. It switches back to "top frame" – bryc Apr 01 '15 at 03:07
  • How I can do the same in IE? I find it extremely difficult to select an element in the console window using $ if the element is located in an inner IFRAME. Please help. – tarekahf May 26 '17 at 16:08
23

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

window.frames[0].runFunction()
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
LoveAndCoding
  • 7,857
  • 2
  • 31
  • 55
7
cd(document.getElementsByTagName('iframe')[0]);
SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
  • 2
    Welcome to Stack Overflow! Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Sep 02 '15 at 13:35
  • Thanks! This is an odd way for firefox to do it, but I guess "cd(iframe)" is what you enter in the console to switch context. I gave you a +1 b/c you saved me tons of head-banging, but you should really polish up this post and explain it! – Scott Smith Jan 30 '17 at 08:04
6

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

dpercy
  • 459
  • 6
  • 13
4

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() Calling cd() without parameters returns to the top-level window.

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.

More info, here

Sebastian
  • 51
  • 2
4

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.

Community
  • 1
  • 1
Muhd
  • 24,305
  • 22
  • 61
  • 78
1

For people using firefox, cd() does not work.

Instead, a possible workaround is to call eval() on the iframe's contentWindow to run javascript in the context of the iframe.

var frame = document.getElementById("MyIframe").contentWindow;
frame.eval("alert(1);");
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Carter
  • 59
  • 1
  • 2
  • 3
  • It doesn't work for me, I get an `Uncaught DOMException: Permission denied to access property "eval" on cross-origin object` even disabling cross-origin validations in the about:config (Firefox). – ffernandez Apr 22 '23 at 16:51