www.webdeveloper.com
Results 1 to 3 of 3

Thread: window.onload with two functions / question from a beginner

  1. #1
    Join Date
    Oct 2011
    Posts
    5

    window.onload with two functions / question from a beginner

    I'm new to Javascript and hope that someone can help me!

    I know that it is not possible to have two window.onload functions at the same time. I tried to figure out how to exactely rewrite the following piece of code so that both functions will work.
    Code:
    <script type="text/javascript">
    
    function liamEfas() {	if (document.getElementById) {
    		var alltags = document.all? document.all : document.getElementsByTagName("*");
    		for (i=0; i < alltags.length; i++) {
    		  if (alltags[i].className == "liamEfas") {
    			var oldText = alltags[i].firstChild;
    			var emailAddress = alltags[i].firstChild.nodeValue;
    			var user = emailAddress.substring(0, emailAddress.indexOf("("));
    			var website = emailAddress.substring(emailAddress.indexOf(")")+1, emailAddress.length);
    			var newText = user+"@"+website;
    			var a = document.createElement("a");
    			a.href = "mailto:"+newText;
    			var address = document.createTextNode(newText);
    			a.appendChild(address);
    			alltags[i].replaceChild(a,oldText);
    		  }
    		}
    	}
    }
    window.onload = liamEfas;
    
    var scroller = null;
    window.onload = function () {
      var el = document.getElementById("scroller");
      scroller = new jsScroller(el, 480, 610);
    }
    
    </script>
    Last edited by Kor; 10-25-2011 at 06:11 AM. Reason: wrap the code [code][/code]

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You have already the solution under your nose. Place everything you want to launch onload within the same function:

    Remove this:
    Code:
    window.onload = liamEfas;
    and call the function like:
    Code:
    window.onload = function () {
    liamEfas();
    var el = document.getElementById("scroller");
    scroller = new jsScroller(el, 480, 610);
    }
    An event can not be double (or multiple) applied if handled within the same element.
    Last edited by Kor; 10-25-2011 at 06:14 AM.

  3. #3
    Join Date
    Oct 2011
    Posts
    5
    Thank You Very Much!

    Although the solution was under my nose, it felt like some accacradaba.
    I'm getting eager to learn JS.

    Thanks again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles