-1

The function below is called as soon as the body is loaded.

document.body.onload = function(){console.log('loaded!')}

I want this same code to be executed, however, by inserting it in the URL bar of the Google Chrome browser.

javascript:function f1(){
    window.location.href='https://www.w3schools.com/'};
    function f2(){
        document.body.onload = function(){
            console.log('loaded!')
        }
    };
    f1();
    f2();

The f2 function should only be called when the f1 function is fully executed. I imagine this is enough for console.log() to be called successfully.

John Lima
  • 85
  • 4
  • 1
    _“The f2 function should only be called when the f1 function is fully executed”_ - that would be the case more or less immediately after you called `f1`. But since you assigned a new location in there, the browser is going to a different page, and that means it will stop executing any additional JS in the current one. And if you switch to a page from a different origin, then you don’t even have access to the body or document of that new page any more, the Same Origin Policy prevents that. – CBroe Jan 04 '21 at 15:00
  • You need to check the URL into simple page load and execute the 2nd one if the URL matches the condition. – Onkar Jan 04 '21 at 15:00
  • 1
    Does this answer your question? [Javascript bookmarklet go to URL and execute](https://stackoverflow.com/questions/7103996/javascript-bookmarklet-go-to-url-and-execute) – Ivar Jan 04 '21 at 15:03

1 Answers1

1

You can't call a function after changing the window.location.href value

scripts will be reloaded from the new page.