Marià
Marià

Reputation: 263

Javascript execute js on a web page such like chrome console

Hi guys i have a question, if i'm on a external web page such as google.com and i want to click on a "whatever" button i open the chrome console and use :

$('input[name="whatever"]').click();

and the button get clicked. it works. there is a way to execute the same command on for instance google.com but not from the console ? like open it in a document like window.open("google.com") and click the button the same way on the web page? thk so much :D

Upvotes: 0

Views: 902

Answers (1)

roccobarbi
roccobarbi

Reputation: 300

No, that is not how Javascript is meant to work in the browser. Once the browser starts opening a new document, any code that hasn't been executed yet in the old one stops running and a totally new environment is loaded.

You can experience it firsthand by first running this code in the console and waiting:

window.setTimeout(console.log, 5000, 'hi');

After 5 seconds, the message 'hi' will display. Then try this:

function test() {
    document.location.href = 'http://www.google.com';
    window.setTimeout(console.log, 5000, 'ciao');
}
test();

Gooogle will load and no message will be shown in the console. Basically, your code was aborted and had no access to the new page.

Upvotes: 1

Related Questions