By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

IE JScript Runtime Error "Object Expected"

RMWChaos
100+
P: 137
WinVista/IE7

I am getting some weird errors only in IE7, but not in FF2.0.0.8 or NN9. It even happens on this website when I click "Sign In". The error is:

"A Runtime Error has occurred."
"Line:xxx"
"Error: Object expected"

and Debug says: "Microsoft JScript runtime error: Object expected."

The line in my code that debug points to is:

Expand|Select|Wrap|Line Numbers
  1. loadDiv("divlogin");
  2.  
I guess there is something fundamentally different between how JScript and Javascript behave (duh!), but I just cannot figure out what is wrong.

The code is relatively simple. "loadDiv(id)" simply creates a 'div' element in 'document.body' with an id, as you would expect, of object "id". What could be simpler? But to be sure, here is my loadDiv() code:

Expand|Select|Wrap|Line Numbers
  1. function loadDiv(id)
  2.  
  3.     {
  4.  
  5.     var elemDiv = document.createElement('div');
  6.  
  7.     elemDiv.id = id;
  8.  
  9.     document.body.appendChild(elemDiv);
  10.  
  11.     };
  12.  
Like I said, very simple. Perhaps I need to do this a different way. Does IE prefer the code like this instead:

Expand|Select|Wrap|Line Numbers
  1. function loadDiv(id)
  2.  
  3.     {
  4.  
  5.     var elemDiv = document.createElement('div');
  6.  
  7.     elemDiv.setAttribute("id", id);
  8.  
  9.     document.body.appendchild(elemDiv);
  10.  
  11.     };
  12.  
Is one way better than another?

Thanks.
Oct 23 '07 #1
Share this Question
Share on Google+
10 Replies


100+
P: 210
WinVista/IE7

I am getting some weird errors only in IE7, but not in FF2.0.0.8 or NN9. It even happens on this website when I click "Sign In". The error is:

"A Runtime Error has occurred."
"Line:xxx"
"Error: Object expected"

and Debug says: "Microsoft JScript runtime error: Object expected."

The line in my code that debug points to is:

Expand|Select|Wrap|Line Numbers
  1. loadDiv("divlogin");
  2.  
If that is definitely the line, then it can only mean that the function is undefined. Are you sure there's nothing in the Firefox console?
Oct 24 '07 #2

Ferris
100+
P: 101
hi

I have test your function "loadDiv" in firefox and IE7, it's fine.

I think the problem is "loadDiv("divlogin");". the browser can't find loadDiv funcion.I think you need to provide more code.

by the way,I guess there should have an error in firefox.firefox will not alert an error message,and the error message will be saved in "Error Console".you can check it in "tool"->"Error Console".
Oct 24 '07 #3

RMWChaos
100+
P: 137
I've been running Firebug. No errors reported in FF at all. But I think I know what the problem is.

I am using addEvent to initiate my external scripts including my loadDOM.js script, which has my loadDiv() function in it, and any function called by addEvent too soon doesn't find it defined. If I wait and run loadDiv say with an onclick="" attrib, it works. So this is a timing issue...I'm firing off loadDiv() commands too soon.

So I suppose I need to add a conditional if(loadDiv) or put my loadDOM code into my loadInit.js file rather than call it from addEvent. Dunno, have to consider this a bit.

Thanks for your replies!
Oct 24 '07 #4

Ferris
100+
P: 101
hi

I don't think you need to change the code in loadDiv because you didn't recieve any error message in Firefox.I guess maybe you used "addActionListener" to add an event to your button. or maybe you can just move your functions in the right position.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <head>
  4.     function  loadDiv....
  5.     function  load2....
  6. </head>
  7.  
  8. <body>
  9.     <input type = "button" id="btn" .../>
  10. </body>
  11.  
  12.     function addEvent()
  13.     {
  14.         loadDiv();
  15.         load2();
  16.         ....
  17.     }
  18.  
  19.     document.getElementById("btn").onclick = addEvent;
  20. <html>
  21.  


hope it helps.
Oct 24 '07 #5

RMWChaos
100+
P: 137
Thanks Ferris, and yes, addEvent() does use addEventListeners, but I don't load any code in my HTML except a link in HEAD to my loadInit.js file. Additionally, all my scripts are broken up into different files in order to reduce bandwidth needs and speed up response time (on-demand javascripts). So really, my problem is much more complicated than I made it out to be. Here's the basic structure of my site:

Single webpage:

HTML calls loadInit.js file, which contains addEvent() functions.

loadInit.js calls all my other scripts using addEvent() for onload, onunload, etc.

Some of my scripts are:

loadDOM.js - contains all my DOM element creation and removal functions.

loadXHR.js - contains my XMLHttpRequest functions.

loadLogin.js - contains my login verification and processing functions. This is where my problem is at the moment.

So either my loadDOM functions are not being loaded into the global space where my other functions can access them, or my other functions like those in loadLogin are loading too quicky / too early before loadDOM is loaded. I am working on testing this now by manually initiating loadLogin with an onclick="" after the page loads (loadDOM runs at window onload at the moment).

I don't know if my addEvent functions initiate in the order they are listed. I need to research that. It's not my code.

I should add that my addEvent function does far more than just control the adding of events. It controls the the browser cache as well to address memory leaks in IE through a removeEvent function and an EventCache function. I can always post the script here if you are interested in seeing it. It's called "Rock Solid addEvent()".

Because addEvent does more than just load up scripts after the page has loaded, I don't know how it affects the loading of functions and vars into global space.
Oct 24 '07 #6

Ferris
100+
P: 101
Hi

it sounds complicated..

but if the addEventListener remain in your code,your code will never be correct in IE,because addEventListener is only supported in Firefox,not in IE.


you can try to change addEventListener into some other code,for example

document.getElementById("btn_ok").addEventListener ('click',func,false);
->
document.getElementById("btn_ok").onclick=func;


if the error still exsist, it is really complicated..

hope it helps. :)
Oct 24 '07 #7

RMWChaos
100+
P: 137
At the moment, I have stopped using Add Event until I get all my independent scripts working, which I almost have. Once I do that, I will reintroduce Add Event.

In the meantime, here's the first part of the Add Event script, which doesn't include the removeEvent() or EventCache() functions. As you can see, the code forks depending on the browser's capabilities:

Expand|Select|Wrap|Line Numbers
  1. function addEvent(obj, type, fn)
  2.  
  3.     {
  4.  
  5.     if (obj.addEventListener) // FireFox, NN, Mozilla, etc.
  6.  
  7.         {
  8.  
  9.         obj.addEventListener(type, fn, false);
  10.  
  11.         // bugzilla bug #241518
  12.  
  13.         EventCache.add(obj, type, fn);
  14.  
  15.         }
  16.  
  17.     else if (obj.attachEvent) // MSIE
  18.  
  19.         {
  20.  
  21.         var func = function()
  22.  
  23.             {
  24.  
  25.             fn.apply(window.event.srcElement);
  26.  
  27.             };
  28.  
  29.         obj.attachEvent("on" + type, func);
  30.  
  31.         EventCache.add(obj, type, func);
  32.  
  33.         }
  34.  
  35.     else if (typeof obj['on' + type] != 'function')
  36.  
  37.         {
  38.  
  39.         obj['on' + type] = fn;
  40.  
  41.         }
  42.  
  43.     else // really old
  44.  
  45.         {
  46.  
  47.         var oldonload = obj['on' + type];
  48.  
  49.         obj['on' + type] = function()
  50.  
  51.             {
  52.  
  53.             oldonload();
  54.  
  55.             fn();
  56.  
  57.             };
  58.  
  59.         };
  60.  
  61.     };
  62.  
Oct 24 '07 #8

RMWChaos
100+
P: 137
And just in case anyone is interested (for you code junkies out there who get all excited just over the chance at looking over code), here is the rest of the Rock Solid addEvent() script:

By the way, a question: can anyone explain to me the reason for the ' (); ' at the very end? You can read more about the original code on Mark Wubben's Site.

Expand|Select|Wrap|Line Numbers
  1. function removeEvent(obj, type, fn)
  2.  
  3.     {
  4.  
  5.     EventCache.remove(obj, type, fn);
  6.  
  7.     };
  8.  
  9.  
  10. var EventCache = function()
  11.  
  12.     {
  13.  
  14.     var listEvents = [];
  15.  
  16.     return {
  17.  
  18.         listEvents : listEvents,
  19.  
  20.         add : function(node, sEventName, fHandler)
  21.  
  22.             {
  23.  
  24.             listEvents.push(arguments);
  25.  
  26.             },
  27.  
  28.         remove : function(node, sEventName, fHandler)
  29.  
  30.             {
  31.  
  32.             var i, item;
  33.  
  34.             for(i = listEvents.length - 1; i >= 0; i = i - 1)
  35.  
  36.                 {
  37.  
  38.                 if(node == listEvents[i][0] && sEventName == listEvents[i][1] && fHandler == listEvents[i][2])
  39.  
  40.                     {
  41.  
  42.                     item = listEvents[i];
  43.  
  44.                     if(item[0].removeEventListener)
  45.  
  46.                         {
  47.  
  48.                         item[0].removeEventListener(item[1], item[2], item[3]);
  49.  
  50.                         };
  51.  
  52.                     if(item[1].substring(0, 2) != "on")
  53.  
  54.                         {
  55.  
  56.                         item[1] = "on" + item[1];
  57.  
  58.                         };
  59.  
  60.                     if(item[0].detachEvent)
  61.  
  62.                         {
  63.  
  64.                         item[0].detachEvent(item[1], item[0][sEventName+fHandler]);
  65.  
  66.                         };
  67.  
  68.                     item[0][item[1]] = null;
  69.  
  70.                     };
  71.  
  72.                 };
  73.  
  74.             },
  75.  
  76.         flush : function()
  77.  
  78.             {
  79.  
  80.             var i, item;
  81.  
  82.             for(i = listEvents.length - 1; i >= 0; i = i - 1)
  83.  
  84.                 {
  85.  
  86.                 item = listEvents[i];
  87.  
  88.                 if(item[0].removeEventListener)
  89.  
  90.                     {
  91.  
  92.                     item[0].removeEventListener(item[1], item[2], item[3]);
  93.  
  94.                     };
  95.  
  96.                 if(item[1].substring(0, 2) != "on")
  97.  
  98.                     {
  99.  
  100.                     item[1] = "on" + item[1];
  101.  
  102.                     };
  103.  
  104.                 if(item[0].detachEvent)
  105.  
  106.                     {
  107.  
  108.                     item[0].detachEvent(item[1], item[2]);
  109.  
  110.                     };
  111.  
  112.                 item[0][item[1]] = null;
  113.  
  114.                 };
  115.  
  116.             }
  117.  
  118.         };
  119.  
  120.     }();
  121.  
Oct 24 '07 #9

Ferris
100+
P: 101
Hi

I think there's no problem in your event code...
Oct 25 '07 #10

RMWChaos
100+
P: 137
Good to hear, thanks Ferris. Once I get all the kinks out of my scripts, I will go back and try to initiate them with addEvent() and see what happens.

By the way, I confirmed with the script's co-creator that in the current script there is no way to force addEvents to load in any particular order. So I guess I will have to come up with a way to do it...perhaps as simple as one script calls the next, or one script has to wait on the one before it to complete loading before firing off. I dunno, still have to think some more on this.
Oct 25 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.