# Running JS Functions from window.onload

**URL:** <https://forum.kirupa.com/t/running-js-functions-from-window-onload/261659>\
**Category:** web dev\
**Created:** [May 29, 2008, 3:35pm UTC](https://forum.kirupa.com/t/running-js-functions-from-window-onload/261659 "2008-05-29T15:35:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wyclef](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@wyclef](https://forum.kirupa.com/u/wyclef)\
**Post date:** [May 29, 2008, 3:35pm UTC](https://forum.kirupa.com/t/running-js-functions-from-window-onload/261659/1 "2008-05-29T15:35:04Z")

</div>

I’m trying to run these two functions onload and don’t want the onload command in the body tag and am wondering if the way I set it up at the bottom is correct. It works fine but i’m not sure it’s the right way.

```auto
/* ================================================================ 
This copyright notice must be untouched at all times.

The original version of this script and the associated (x)html
is available at http://www.stunicholls.com/menu/paged_menu.html
Copyright (c) 2005-2007 Stu Nicholls. All rights reserved.
This script and the associated (x)html may be modified in any 
way to fit your requirements.
=================================================================== */

rollMenu = function(menu) {
	var getEls = document.getElementById(menu).getElementsByTagName('LI');
	var getAgn = getEls;

	for (var i=0; i<getEls.length; i++) {
			getEls*.onmouseover=function() {
				for (var x=0; x<getAgn.length; x++) {
				getAgn[x].className=getAgn[x].className.replace('roll', '');
				}
				this.className+=' roll';
				}
			}
		}

//-- ABBR Style, $Revision: 1.0 $,
//-- Courtesy of A List Apart: www.sovavsiti.cz/css/abbr.html

function styleAbbr() {
	var oldBodyText, newBodyText, reg
	if (isIE) {
		oldBodyText = document.body.innerHTML;
		reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
		newBodyText = oldBodyText.replace(reg, '<ABBR $1><SPAN class=\"abbr\" $1>$2</SPAN></ABBR>');
		document.body.innerHTML = newBodyText;
	}
}

window.onload = function() {
  styleAbbr()
  rollMenu('menu')
};
  
isIE = (document.all) ? true:false;

```

---

<div class="post-metadata">

**Author:** ![fasterthanlight](https://avatars.discourse-cdn.com/v4/letter/f/ecc23a/32.png) [@fasterthanlight](https://forum.kirupa.com/u/fasterthanlight)\
**Post date:** [May 29, 2008, 3:45pm UTC](https://forum.kirupa.com/t/running-js-functions-from-window-onload/261659/2 "2008-05-29T15:45:46Z")

</div>

instead of window.onLoad = funtion () { }

I would define a function that calls your two functions normally,

and then call that in your .onLoad

init = function () {  
styleAbbr()  
rollMenu(‘menu’)  
}

window.onLoad = init;

> **[JavaScript Programming](https://www.thoughtco.com/javascript-programming-4133476)**
>
> Take your web pages to the next level with interactive JavaScript elements. Find tutorials, how-tos, sample scripts, and more to help you learn to write your own JavaScript code.

the way you have it now _should_ work me thinks…

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [May 29, 2008, 4:20pm UTC](https://forum.kirupa.com/t/running-js-functions-from-window-onload/261659/3 "2008-05-29T16:20:10Z")

</div>

edit: Link had an example that didn’t work. tried it 😕 removed it. Sori!

---

<div class="post-metadata">

**Author:** ![wyclef](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@wyclef](https://forum.kirupa.com/u/wyclef)\
**Post date:** [May 29, 2008, 7:28pm UTC](https://forum.kirupa.com/t/running-js-functions-from-window-onload/261659/4 "2008-05-29T19:28:50Z")

</div>

yea, that didnt work

---

<div class="post-metadata">

**Author:** ![Syous](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Syous](https://forum.kirupa.com/u/Syous)\
**Post date:** [May 29, 2008, 7:46pm UTC](https://forum.kirupa.com/t/running-js-functions-from-window-onload/261659/5 "2008-05-29T19:46:48Z")

</div>

Why can’t you have it in the body tag?

---

<div class="post-metadata">

**Author:** ![wyclef](https://avatars.discourse-cdn.com/v4/letter/w/65b543/32.png) [@wyclef](https://forum.kirupa.com/u/wyclef)\
**Post date:** [May 29, 2008, 9:29pm UTC](https://forum.kirupa.com/t/running-js-functions-from-window-onload/261659/6 "2008-05-29T21:29:35Z")

</div>

phobia
