简体   繁体   中英

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.

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM