Chrome has the easiest way of doing this by simply selecting a frame from a drop down. Firefox provides the cd(frame)
function which is less usable but does the job as long as you're willing to discover which frame is which...
And specifically in IE8 if this has changed in later versions... The usual way would of course be to write these kind of lines to access particular frame's context:
frames[n].window.somePublicFunction();
But that seems so cumbersome especially when you have nested iframes
. Finding the correct frame is simply a pain.
You can just right-click on the element and select Inspect and DevTools highlights it in the DOM Tree on the Elements panel. In the Console, use the Execution Context Menu to change the context of the Console to the iframe.
We can detect if an element is inside an iframe by inspecting the element with the Chrome Developer Tools. An easier way is to perform a Right Click near the element in your browser and see if View Frame Source option is present in the context dropdown.
Using switchTo().defaultContent() The switchTo(). defaultContent() method will switch focus to the first frame on the page. In the case of iFrames, the context is switched back to the main (or Parent) document.
To set an iframe as the target for the developer tools: Select the iframe context picker button to launch a popup listing all the iframes in the document (and the main document itself). Note that the button is only displayed if the page includes iframes! Select an entry to make it the target iframe.
If there is Javascript already in that frame, then add a breakpoint to that script. When IE hits the breakpoint, the console will run in the context of the frame which contains the code on which the breakpoint was set.
To set a breakpoint:
window.location.href
to see what the current window/frame context is by the URL of the page that is loaded in it.)You can change the context of the JS console in Internet Explorer 11 with the console.cd()
method.
Given this iframe:
<iframe src="http://google.com" frameborder="0" id="someIframeID"></iframe>
You pass an unquoted ID or name of the frame to switch the context to that frame. Execute this command in the console.
console.cd(someIframeID);
To switch the context back to the parent, you just call console.cd();
without any arguments.
Reference: http://msdn.microsoft.com/en-us/library/ie/dn255006(v=vs.85).aspx#console_in
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With