SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Onload listener problemo

    I was reading through soem ajax book i picked up months ago but havent had time to read and I've come across a problem when I try and solve the problem with the window onload function and having various different things that need to be loaded.

    Here's what I've got:

    Code:
    // Onload Observer
    window.onloadListeners = new Array();
    window.addOnloadListener(listener)
    {
    	window.onloadListeners[window.onloadListeners.length] = listener;
    }
    
    // Listeners
    window.addOnloadListener('adjustLayout');
    window.addOnloadListener('externalLinks');
    
    // Onload
    window.onload = function()
    {
    	for (var i = 0; i < window.onloadListeners.length; i++)
    	{
    		var func = window.onloadListeners[i];
    		func.call();
    	}
    }
    When I try to run it though I get the following from my javascript console (go team firefox!):

    Error: listener is not defined
    Source File: http://www.example.com/js/common.js
    Line: 78
    hmm...

  2. #2
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.addOnloadListener = function(listener)
    I've made that mistake a couple of times myself..

    And by the way,
    Code:
    window.addOnloadListener('adjustLayout');
    window.addOnloadListener('externalLinks');
    isn't going to work. I guess you're used to PHP where this would be the correct (and only) way to pass "functions" as parameters. In JS, however, you do not need to do this, you can pass the function directly (which is what you are trying to do). So, drop the quotes.

    Code:
    window.addOnloadListener(adjustLayout);
    window.addOnloadListener(externalLinks);

  3. #3
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply.

    Changed it to:

    Code:
    // Onload Observer
    window.onloadListeners = new Array();
    window.addOnloadListener(listener)
    {
    	window.onloadListeners[window.onloadListeners.length] = listener;
    }
    
    // Listeners
    window.addOnloadListener = function(adjustLayout);
    window.addOnloadListener = function(externalLinks);
    
    // Onload
    window.onload = function()
    {
    	for (var i = 0; i < window.onloadListeners.length; i++)
    	{
    		var func = window.onloadListeners[i];
    		func.call();
    	}
    }
    But then I get this error:

    Error: missing { before function body
    Source File: http://www.example.com/js/common.js
    Line: 84, Column: 49
    hmm...

  4. #4
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried it with:
    Code:
     window.addOnloadListener(adjustLayout);
     window.addOnloadListener(externalLinks);
    but I still get the original error.

    And yeah I'm a PHP guy
    hmm...

  5. #5
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually reread your post and sorted it haha.

    Thansk alot
    hmm...


Bookmarks

Posting Permissions

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