Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Runtime Error...Can't Figure Out Why
Old 08-10-2008, 03:28 AM Runtime Error...Can't Figure Out Why
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Hello.

This is a reference file from a book I read in which the core subject is the use of 'event listeners'.

I'm trying to load the file in Firefox and it's giving me an error message:

Line: 5
Char: 1
Error: 'document' is undefined
Code: 800A1391
Source: Microsoft JScript Runtime Error

I need this file to work because it's the reference file for all of my coding. Here's the contents of the file here.

Code:
var Core={};

// W3C DOM 2 Events model 

if (document.addEventListener)
{
   Core.addEventListener = function(target, type, listener)
    {
      target.addEventListener (type, listener, false);
    };


   Core.removeEventListener = function(target, type, listener)
    {
      target.removeEventListener(type, listener, false);
    };

   Core.preventDefault = function(event)
    {
      event.preventDefault();
    };

   Core.stopPropagation = function(event)
    {
      event.stopPropagation();
    };
}

// Internet Explorer Events model

else if (document.attachEvent)
{
   Core.addEventListener = function(target, type, listener)
   {
     // prevent adding the same listener twice, since DOM 2
      
     // Events ignores duplicates like this

     if (Core._findListener(target, type, listener) != -1)
       return;

    // listener2 calls listener as a method of target in one of

    // two ways, depending on what this version of IE supports,

    // and passes it the global event object as an argument

    var listener2 = function()
    {
      var event = window.event;

      if (Function.prototype.call)
      {
        listener.call(target, event);
      }
     
      else
      {
        target._currentListener = listener; 
        target._currentListener(event)
        target._currentListener = null; 
      }
    };


    // add listener2 using IE's attachEvent method
    target.attachEvent("on" + type, listener2);

    // create an object describing this listener so we can

    // clean it up later
    
    var listenerRecord =
    {
      target: target, 
      type: type, 
      listener: listener, 
      listener2: listener2
    };

    // get a reference to the window object containing target
    
    var targetDocument = target.document || target;
    var targetWindow = targetDocument.parentWindow;

    // create a unique ID for this listener
    
    var listenerId = "1" + Core._listenerCounter++;

    // store a record of this listener in the window object
    
    if (!targetWindow._allListeners)
       targetWindow._allListeners = {};
    targetWindow._allListeners[listenerID] = listenerRecord;

    // store this listener's ID in target

    if (!target._listeners) target._listeners = [];
    target._listeners[target._listeners.length] = listenerId;

    // set up Core._removeAllListeners to clean up all 
    
    // listeners on unload 

    if (!targetWindow._unloadListenerAdded)
    {
      targetWindow._unloadListenerAdded = true;
      targetWindow.attachEvent(
         "onunload", Core._removeAllListeners);
    }
   };


   Core.removeEventListener = function(target, type, listener)
   {
     // find out if the listener was actually added to target

     var listenerIndex = Core._findListener(
         target, type, listener);
     if (listenerIndex == -1) return;

    // get a reference to the window object containing target

    var targetDocument = target.document || target;
    var targetWindow = targetDocument.parentWindow;

    // obtain the record of the listener from the window object
    
    var listenerId = target._listeners[listenerIndex];
    var listenerRecord = 
        targetWindow._allListeners[listenerId];

    // remove the listener, and remove its ID from target

    target.detachEvent("on" + type, listenerRecord.listener2);
    target._listeners.splice(listenerIndex, 1);

    // remove the record of the listener from the window object
    
    delete targetWindow._allListeners[listenerId];

   };

   Core.preventDefault = function(event)
   {
     event.returnValue = false;
   };

   Core.stopPropagation = function(event)
   {
     event.cancelBubble = true; 
   };

   Core._findListener = function(target, type, listener)
   {
     // get the array of listener IDs added to target

     var listeners = target._listeners; 
     if (!listeners) return -1;

     // get a reference to the window object containing target
     
     var targetDocument = target.document || target; 
     var targetWindow = targetDocument.parentWindow;

     // searching backward (to speed up onunload processing),
    
     // find the listener

     for (var i = listeners.length - 1; i >= 0; i--)
     {
      // get the listener's ID from target
      
      var listenerId = listeners[i];

      // get the record of the listener from the window object

      var listenerRecord =
          targetWindow._allListeners[listenerId];

      // compare type and listener with the retrieved record
      
      if (listenerRecord.type == type &&
             listenerRecord.listener == listener)
      {
        return i;
      }
     }
     return -1;
   };

   Core._removeAllListeners = function()
   {
     var targetWindow = this; 

     for (id in targetWindow._allListeners)
     {
       var listenerRecord = targetWindow._allListeners[id];
       listenerRecord.target.detachEvent(
         "on" + listenerRecord.type, listenerRecord.listener2);
       delete targetWindow._allListeners[id];
     }
    };

   Core._listenerCounter = 0;
 
  }


  Core.addClass = function(target, theClass)
  {
    if (!Core.hasClass(target, theClass))
    {
      if (target.className == " ")
      {
        target.className = theClass;
      }
     
      else
      {
        target.className += " " + theClass;
      }
     }

  };


  Core.getElementsByClass = function(theClass)
  {
    var elementArray = [];

    if (typeof document.all != "undefined")
    {
     elementArray = document.all;
    }

    else
    {
      elementArray = document.getElementsByTagName("*");
    }

    var matchedArray = [];
    var pattern = new RegExp("(^| )" + theClass + "( |$)");

    for (var i = 0; i < elementArray.length; i++)
    {
      if (pattern.test(elementArray[i].className))
      {
        matchedArray[matchedArray.length] = elementArray[i];
      }
    }

    return matchedArray;

  };


  Core.hasClass = function(target, theClass)
  {
    var pattern = new RegExp("(^| )" + theClass + "( |$)");

    if (pattern.test(target.className))
    {
      return true;
    }

    return false; 

  };


  Core.removeClass = function(target, theClass)
  {
    var pattern = new RegExp("(^| )" + theClass + "( |$)");

    target.className = target.className.replace(pattern, "$1");
    target.className = target.className.replace(/ $/, " ");
  };


  Core.getComputedStyle = function(element, styleProperty)
  {
    var computedStyle = null;

    if (typeof element.currentStyle != "undefined")
    {
      computedStyle = element.currentStyle;
    }

    else
    {
      computedStyle =
          document.defaultView.getComputedStyle(element, null);
    }

    return computedStyle[styleProperty];

   };

   Core.start = function(runnable)
   {
     Core.addEventListener(window, "load", runnable.init);
   };
LayneMitch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-10-2008, 02:38 PM Can't Open .js file w/ Windows Script Host Error
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Hello.

I posted this earlier with the full code of the JavaScript file I'm trying to open. I'm posting this again, because the file is not the problem.

I seem to be having a 'Windows' problem w/ Window Script Host. While this is more of a computer support based problem, I was hoping that someone had any knowledge on how to resolve this situation. The error message I'm getting is:

Line: 5
Char: 1
Error: 'document' is undefined
Code: 800A1391
Source: Microsoft JScript Runtime Error

Is there any idea on how to resolve this?
LayneMitch is offline
Reply With Quote
View Public Profile
 
Old 08-10-2008, 05:05 PM Re: Can't Open .js file w/ Windows Script Host Error
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
probably file associations on your machine
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-11-2008, 10:37 AM Re: Can't Open .js file w/ Windows Script Host Error
Skilled Talker

Posts: 61
Trades: 0
Can you post a link so we can see what's going on?
__________________

Please login or register to view this content. Registration is FREE

just_a_pod is offline
Reply With Quote
View Public Profile
 
Old 08-13-2008, 12:50 PM Re: Runtime Error...Can't Figure Out Why
Average Talker

Posts: 19
Name: Meir Ech
Trades: 0
LayneMitch - Just looking at your question would take days of study.
Try and narrow things down before you submit your problem.
We'd like to help, but not be overloaded and burdened.
Hope you understand. No need to respond.
ruffy is offline
Reply With Quote
View Public Profile
 
Old 08-13-2008, 01:13 PM Re: Runtime Error...Can't Figure Out Why
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
The document object may be undefined if you are attempting to run this script before the DOM is available (all HTML is available). Either place your script into a function triggered by the onload event, or else place the script at the bottom of your page, right before the closing </body> tag.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 08-13-2008, 05:17 PM Re: Runtime Error...Can't Figure Out Why
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
<merged both threads on the same problem>
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Runtime Error...Can't Figure Out Why
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.11731 seconds with 12 queries