Aleks
Aleks

Reputation: 45

How do I change elements of a target page following a window.open()?

I am creating a button that upon click will open a new window with webpage X, within which there is a search bar that needs to automatically reflect the search "Team."

I already have the document.getElementById() of webpage X, and my button already opens webpage X, but how do I (upon window.open) fill the search bar with "Team" using a script that is being run on the first page.

I hope this is clear, as I am somewhat new to this.

Upvotes: 0

Views: 320

Answers (1)

Vi100
Vi100

Reputation: 4203

When you call window.open(...) you have to assign the result to a variable, then you can use it to manipulate the new window contents, and look for elements the same that you do with the current one. Be aware that you'll have to wait for the new window to be loaded before searching for elements on it, so for your pourpose it's better to put that code into the onload callback:

var theNewWindow = window.open("https://www.youtube.com");
theNewWindow.onload = function() {
    theNewWindow.document.getElementById("masthead-search-term").value = 'team';
};

Upvotes: 1

Related Questions