473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

IE JScript Runtime Error "Object Expected"

RMWChaos
137 100+
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
10 13272
Logician
210 100+
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
101 100+
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
137 100+
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
101 100+
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
137 100+
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
101 100+
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
137 100+
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
137 100+
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
101 100+
Hi

I think there's no problem in your event code...
Oct 25 '07 #10
RMWChaos
137 100+
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

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

Similar topics

4
by: Danny | last post by:
Hi I don't understand why I keep getting "Error: Object expected" I tried different things and haven't been able to solve it. I'm a newbie so I'm not sure what I'm doing wrong. The debugger breaks...
5
by: theroo | last post by:
Hi there, I am driving myself crazy over this problem. I have used this very same function in another page without any problems, but now I am experiencing the "Error: Expected ';'" when i try...
1
by: STAGED | last post by:
The script: proc bugsy { } { global helmet pack forget .widget1 if { $helmet(key1) } { pack .widget2 -anchor nw -side top -expand 0 } else { show_new_widget }
16
by: Steve Chapel | last post by:
When I load the page <https://bugzilla.mozilla.org/attachment.cgi?id=237739with Internet Explorer 7 RC 1, I get the error "Object Expected" at line 174. When I click on the button, I also get the...
1
by: monudjn | last post by:
Hi I am implementing ajax in portal. Using ajax i am able to updating the content of portlets asynchronously. However i am facing a problem The Problem: While submitting the form i am getting...
6
by: Lawrence Spector | last post by:
I ran into a problem using g++. Visual Studio 2005 never complained about this, but with g++ I ran into this error. I can't figure out if I've done something wrong or if this is a compiler bug. ...
5
by: Diego Ruiz | last post by:
Hi!! my english is not too good, but i'll try it I have a javascript code: function executeCommands(inputparms) { // Instantiate the Shell object and invoke its execute method. ...
2
by: thj | last post by:
Hi. I've got this form that I'm trying to validate: <form id="periodForm" action="" method="post"> <p> Periode: <input id="startDate" name="startDate" type="text" size="7" value="<%=...
9
by: Rohit | last post by:
I am trying to initialize an array whose initializers depend on value of Enums. I take enum and then decide the initializer value, so that even if enum value changes because of addition to list...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.