Rusu Traian Cristian Rusu Traian Cristian - 4 days ago 6
Javascript Question

How do I execute a console Javascript on page load?

I have a Javascript which can be ran from Google Chrome console. All it does is to automatically invite all of your Facebook friends to like a page you manage. Is there any way to automate this process so that people won't need to copy and paste the script into the console but rather simply access a website and trigger the script onLoad or onClick when a button is clicked on the website? Many people in my Facebook group own a page although they don't know how to use the console nor an script. I'm beginner at Java and I have found nothing related to this on SoF so far. Thanks.

Answer

Every web console run every JavaScript code. You want your script run when page is loaded. This event occurs when the page just finished to load. When you go to Web console, the page is already loaded. If you tell your page to run a function when the page is loaded, but your page is already loaded, the function will never be called.

Insert your script in your page, or place the script-link, before end of </html>.

Then in your script, call your function ( example fn ) :

window.addEventListener && window.addEventListener( "load", fn ) ||
window.attachEvent && window.attachEvent( "load", fn );
Comments