How to handle Front of another Window with JS

1

I've been researching until I get tired ... what I'd like to do was 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 does not 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?

Anyone help me?

    
asked by anonymous 11.10.2018 / 18:16

2 answers

0

You need to wait for the window to load 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 the location of your machine you will probably take a security permission error, for example "ERR_BLOCKED_BY_CLIENT".

    
11.10.2018 / 18:29
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 into 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.

    
11.10.2018 / 18:32