Best practice for using [removed]

2019-01-02 23:48发布

I develop Joomla websites/components/modules and plugins and every so often I require the ability to use JavaScript that triggers an event when the page is loaded. Most of the time this is done using the window.onload function.

My question is:

  1. Is this the best way to trigger JavaScript events on the page loading or is there a better/newer way?
  2. If this is the only way to trigger an event on the page loading, what is the best way to make sure that multiple events can be run by different scripts?

7条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-03 00:13

The window.onload events are overridden on multiple creations. To append functions use the window.addEventListener(W3C standard) or the window.attachEvent(for IE). Use the following code which worked.

if (window.addEventListener) // W3C standard
{
  window.addEventListener('load', myFunction, false); // NB **not** 'onload'
} 
else if (window.attachEvent) // Microsoft
{
  window.attachEvent('onload', myFunction);
}
查看更多
登录 后发表回答