9

I know that Chrome let's you pick the context for the console's execution with a dropdown menu and that Firebug let's you cd() into an iframe. I can't figure out how to change the context in Safari's console. Does anyone know how to do this?

Community
  • 1
  • 1
Aaron Gibralter
  • 4,773
  • 3
  • 35
  • 50

2 Answers2

9

Safari, unlike chrome and firefox, has no real support for this functionality and the only option seems to be to access the window object from the console. As you correctly point this will trigger cross domain policy issues, however provided you're running on mac (this does not work for some reason on windows) you can use

open -a '/Applications/Safari.app' --args --disable-web-security

to bypass this. And next on your jsbin you could use something along the lines of

window.frames[0]

to access the window of the page. As far as I can see there is no similar solution for windows, as

Safari.exe --disable-web-security

apparantly does not work.

David Mulder
  • 26,123
  • 9
  • 51
  • 114
0

The Iframe element itself is of Type Window within Console

<iframe id="frame" src="about:blank"/>

In Safari console then you simply work with

frame.document.write('bla');

please notice that 'frame' is shorthand for document.getElementById('frame')

mr.VVoo
  • 2,265
  • 20
  • 30
  • Unfortunately that does not work with iframes with a different domain than the host page. That's why the firebug `cd` command and the Chrome drop down menu for changing "contexts" are so useful... – Aaron Gibralter Feb 20 '13 at 21:34
  • Sure? I tried this with a different URL and it worked. Only for posting I changed this to about:blank. – mr.VVoo Feb 20 '13 at 23:30
  • Yup: http://jsbin.com/ojiley/2 -- when I open the Safari console and type: `document.getElementById("test-iframe").document` I get `undefined`: http://cl.ly/image/0N0v1l223w1m – Aaron Gibralter Feb 21 '13 at 15:15
  • try `document.getElementById("test-iframe").contentDocument` or `document.getElementById("test-iframe").contentWindow`. additionally you can print all parts of the frame to the console: `for(var key in document.getElementById("test-iframe")) console.log(key);`in order to find all useful attributes. – mr.VVoo Feb 21 '13 at 15:21
  • Nope, I still cannot access anything within the iframe. Are you sure you're using Safari 6.x? – Aaron Gibralter Feb 21 '13 at 16:19
  • Yes of course. Safari 6 on OS X Mountain Lion. And i tried them and i had access to the document object of the iframe. – mr.VVoo Feb 21 '13 at 16:59
  • Hmm @mr.VVoo can you tell me what code you've type into the Safari Web Inspector console to be able to access a dom element on the Amazon.com page in the iframe? – Aaron Gibralter Feb 21 '13 at 20:33
  • here's the screenshot http://imageshack.us/photo/my-images/833/bildschirmfoto20130221u.png/ – mr.VVoo Feb 21 '13 at 20:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24935/discussion-between-aaron-gibralter-and-mr-vvoo) – Aaron Gibralter Feb 21 '13 at 22:58