DOM manipulation of window.open?

Hi! how can I manipulate the DOM of the popup window. for example this code,

var test = window.open("http://google.com","test","width=500,height=400")
test.opener.document.getElementsByClassName('gsfi')["0"].value = "test"

is there any solution where I could put the “test” value to search box on google?

Thank you!

well test.opener.document makes no sense for manipulating something on the popup, cause… the opener is the main window.

Secondly, you’ll have to wait until the popup window is done loading the page you’d told it to open, or the DOM elements you’re trying to manipulate won’t exist yet.

try test.window.onload = function() { test.document.getElementsByClassName('gsfi')["0"].value = "test" } instead.

Hi m_hutley thank you for your response, alright I got it now. however the code you sent is still not working?? I don’t know if its possible.

I don’t think you can interact with the window that’s opened the way you’re trying.

… because of the cross-domain script security restriction (also referred as the “Same Origin Policy”). A script loaded in a window (or frame) from a distinct origin (domain name) cannot get nor set properties of another window (or frame) or the properties of any of its HTML objects coming from another distinct origin (domain name). Therefore, before executing a script targeting a secondary window, the browser in the main window will verify that the secondary window has the same domain name.

If you are trying to pre-populate the input, what you could do instead is try appending the query string to the URL in the open. i.e.

https://www.google.com/search?q=sitepoint

https://www.google.com/search?q=sitepoint

* be sure to urlencode it if it needs to be.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.