How to manipulate another Window Front with JS

Asked

Viewed 47 times

1

I’ve done enough research... what I’d like to do is something like:

var janela = window.open(
    "https://www.google.com/", 
    "_blank"
);
var pesquisa = janela.document.getElementById("lst-ib");
pesquisa.value = "string que quero no input";

but it doesn’t work, I put:

var janela = window.open(
    "https://www.google.com/", 
    "_blank"
);
var pesquisa = janela.document.getElementById("lst-ib");
console.log(pesquisa);

printed null

WHY?

somebody help me?

2 answers

0


You need to wait for the window to be loaded so that the element is available and you can change it. You can do this by treating the event onload:

var janela = window.open(
    "https://www.google.com/", 
    "_blank"
);
janela.onload = function(){
    var pesquisa = janela.document.getElementById("lst-ib");
    pesquisa.value = "string que quero no input";
}

If you are testing your machine’s location will probably take a security permission error, for example "ERR_BLOCKED_BY_CLIENT".

0

    var pesquisa = "string que quero no input";
    var janela = window.open(
        "https://www.google.com/?q=" + pesquisa, 
        "_blank"
    );

See if this helps, the text you put in search will enter as a GET in the google domain and with the field filled, but the search is not done you need to select search.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.