943,946 Members | Top Members by Rank

Ad:
Jul 9th, 2009
0

IE Error (as per the norm): Object Expected

Expand Post »
As usual, develop something in FF and as soon as you get near IE everything decides to pack up and go home.

I'm getting an object expected error, forgive me if it's simple, I'm new to JavaScript.

HTML Code
html Syntax (Toggle Plain Text)
  1. <div id="tabsCon" class="tabsContainer">
  2. <span id="tab1" class="activeTab" onclick="javascript:changeTab('tab1');">Tab 1</span>
  3. <span id="tab2" class="inactiveTab" onclick="javascript:changeTab('tab2');">Tab 2</span>
  4. <span id="tab3" class="inactiveTab" onclick="javascript:changeTab('tab3');">Tab 3</span>
  5. <span id="tab4" class="inactiveTab" onclick="javascript:changeTab('tab4');">Tab 4</span>
  6. <span id="tab5" class="inactiveTab" onclick="javascript:changeTab('tab5');">Tab 5</span>
  7. <span id="tab6" class="inactiveTab" onclick="javascript:changeTab('tab6');">Tab 6</span>
  8. </div>

JavaScript
javascript Syntax (Toggle Plain Text)
  1. function changeTab(tab){
  2. tabCont = document.getElementById('tabsCon');
  3. items = tabCont.getElementsByTagName('span');
  4. var len = items.length;
  5. for (i=1;i<=len;i++){
  6. if (document.getElementById('tab'+i).getAttribute('class') == "activeTab" || document.getElementById('tab'+i).getAttribute('className' == "activeTab")
  7. document.getElementById('tab'+i).setAttribute("class", "inactiveTab");
  8. document.getElementById('tab'+i).setAttribute("className", "inactiveTab");
  9. }
  10. obj2 = document.getElementById(tab);
  11. obj2.setAttribute("class", "activeTab");
  12. obj2.setAttribute("className", "activeTab");
  13. http.open('get', page+'?task=changeTab&tab='+tab);
  14. http.onreadystatechange = handleResponse;
  15. http.send(null);
  16. }

There is obviously more to the .js file but I've included what matters.
As I say, it's all working in firefox, any help would be greatly appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dalefish is offline Offline
16 posts
since Jan 2008
Jul 9th, 2009
0

Re: IE Error (as per the norm): Object Expected

My guess is that IE is behaving correctly in that it is rejecting "javascript:" in those onclicks. I am amazed that FF handles it ok as I believe it is not obliged to.

With onclick, onmouseover, onmouseout etc. etc. browsers know to expect javascript. Little appreciated, you implicitly write an anonymous javascript function.

You should never need to do it (it is bad practice), but code written into an HTML href attribute does need the "javascript:" prefix. This is because browsers know to expect a normal (hyperlink) URL and need to be told otherwise.

Airshow
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,527 posts
since Apr 2009
Jul 9th, 2009
0

Re: IE Error (as per the norm): Object Expected

Thanks for the reply.

Unfortunatley that doesn't seem to be it, unless I've completely lost my way.
HTML Syntax (Toggle Plain Text)
  1. <span id="tab1" class="activeTab" onclick="changeTab('tab1')">Tab 1</span>
HTML Syntax (Toggle Plain Text)
  1. <span id="tab1" class="activeTab" onclick="changeTab('tab1');">Tab 1</span>
Both still giving errors. Is my application of your comment incorrect, if so please tell me. As I say, very new to JavaScript.

As a side note, on a different site I've got other JS functions in onclick attributes running AJAX updates, example:
html Syntax (Toggle Plain Text)
  1. <a href="#stay" onclick="javascript:revokeInvite('11','81');">
Working fine in both IE and FF.

Any ideas greatly appreciated.

Cheers for the help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dalefish is offline Offline
16 posts
since Jan 2008
Jul 10th, 2009
0

Re: IE Error (as per the norm): Object Expected

Dale,

Well it was a good theory while it lasted. You appear to have done exactly what I suggested.

Does IE give you a line number where the error occurs? That would help. If you don't see error messages then double click the yellow !triangle bottom-left (assuming your version of IE is anything like 6) - otherwise you'll have to dive into Internet Options > Advanced (I think) to turn on js error reporting.

NOTE: If js code is in a separate file then IE's standard error messages have a bad habit of reporting line numbers as one too great, and also report them as being in the host file not the 'included' file!!

It could be that the code is breaking down within changeTab() rather than where it is called from the HTML.

Airshow
Last edited by Airshow; Jul 10th, 2009 at 8:43 am.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,527 posts
since Apr 2009
Jul 10th, 2009
0

Re: IE Error (as per the norm): Object Expected

Airshow,

Thanks for the reply, I was about to mark the post solved.

Turns out it was a completely unrelated error about 200 lines below the changeTab() function in the js file. Using a reserved word as a function argument, quick change and all is good.

Well I say all is good, the function the error was in still doesn't work in IE, but at least it runs in FF and IE compiles the script. I'm running a setTimeout to change (slide) a span style from width:0 and visibility:hidden to a specific width, generated before width:0 is added with offsetWidth, and visibility:visible. Simple sliding span, or so I thought. C'est la vie, it's not throwing any errors so I'm going to do a little trial and error, see if I can't work it out and learn something.

Cheers for the help.
Last edited by dalefish; Jul 10th, 2009 at 8:54 am. Reason: Spelling (take 1)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dalefish is offline Offline
16 posts
since Jan 2008
Jul 10th, 2009
0

Re: IE Error (as per the norm): Object Expected

Dale,

Good luck. Visual effects like that are very satisfying to get working.

Airshow
Last edited by Airshow; Jul 10th, 2009 at 1:28 pm.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,527 posts
since Apr 2009
Nov 1st, 2009
0
Re: IE Error (as per the norm): Object Expected
Try putting this in <head> section:
<meta http-equiv="Content-Script-Type" content="text/javascript">

I had a similar problem with IE 7, upgraded to IE 8, and had same
problem, with following line:

<input type="text" name="myText" onKeyPress="changeVal()" value="new">

IE did not recognize changeVal() as an object. Unbelievable, yet typical MS crap. Of course, FF had no problem.

If you are only using javascript scripts, this is fine and you do not have to qualify the <script> tag.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
erect1kus is offline Offline
1 posts
since Nov 2009
Nov 4th, 2009
0
Re: IE Error (as per the norm): Object Expected
Click to Expand / Collapse  Quote originally posted by dalefish ...
As usual, develop something in FF and as soon as you get near IE everything decides to pack up and go home.

I'm getting an object expected error, forgive me if it's simple, I'm new to JavaScript.

HTML Code
html Syntax (Toggle Plain Text)
  1. <div id="tabsCon" class="tabsContainer">
  2. <span id="tab1" class="activeTab" onclick="javascript:changeTab('tab1');">Tab 1</span>
  3. <span id="tab2" class="inactiveTab" onclick="javascript:changeTab('tab2');">Tab 2</span>
  4. <span id="tab3" class="inactiveTab" onclick="javascript:changeTab('tab3');">Tab 3</span>
  5. <span id="tab4" class="inactiveTab" onclick="javascript:changeTab('tab4');">Tab 4</span>
  6. <span id="tab5" class="inactiveTab" onclick="javascript:changeTab('tab5');">Tab 5</span>
  7. <span id="tab6" class="inactiveTab" onclick="javascript:changeTab('tab6');">Tab 6</span>
  8. </div>

JavaScript
javascript Syntax (Toggle Plain Text)
  1. function changeTab(tab){
  2. tabCont = document.getElementById('tabsCon');
  3. items = tabCont.getElementsByTagName('span');
  4. var len = items.length;
  5. for (i=1;i<=len;i++){
  6. if (document.getElementById('tab'+i).getAttribute('class') == "activeTab" || document.getElementById('tab'+i).getAttribute('className' == "activeTab")
  7. document.getElementById('tab'+i).setAttribute("class", "inactiveTab");
  8. document.getElementById('tab'+i).setAttribute("className", "inactiveTab");
  9. }
  10. obj2 = document.getElementById(tab);
  11. obj2.setAttribute("class", "activeTab");
  12. obj2.setAttribute("className", "activeTab");
  13. http.open('get', page+'?task=changeTab&tab='+tab);
  14. http.onreadystatechange = handleResponse;
  15. http.send(null);
  16. }

There is obviously more to the .js file but I've included what matters.
As I say, it's all working in firefox, any help would be greatly appreciated.
You are kidding, right?!
No matter what broswer you test this on, will not work at all.
Although invisible to novice attention. What 'explorer'?! It contains errors, javascript errors, therefore your statement is false. It shall faill in fx and any other browser.
But this code should not:
function changeTab(tab){
	var tabCont = document.getElementById('tabsCon');
	var items = tabCont.getElementsByTagName('span');
	var len = items.length;
	for (i=1;i<=len;i++){
  if (document.getElementById('tab'+i).getAttribute('class') == "activeTab" || document.getElementById('tab'+i).getAttribute('className' ==  "activeTab")){
	document.getElementById('tab'+i).setAttribute("class", "inactiveTab");
	document.getElementById('tab'+i).setAttribute("className", "inactiveTab") }
	}
	var	obj2 = document.getElementById(tab);
		obj2.setAttribute("class", "activeTab");
		obj2.setAttribute("className", "activeTab");
/*		http.open('get', page+'?task=changeTab&tab='+tab);
		http.onreadystatechange = handleResponse;
		http.send(null);
*/	}

[! I've isolated last lines of code because its externaly dependant]
Colored parts are all errors; from red hot to a warning green.
Your main loop conditional evaluator is not closed, so the addressed object fails to get picked. So here we are, the error report is correct!
You are also violating global space with undeclared variable names causing mixed scope reference. Your declared "len" variable is not sharing the same space with two previously undeclared 'vars' which in fact, will not be treated as vars but properties of the top host scripting object.
The last but not the least is missing {} after the loop conditional. It is legal, but not safe.
p.s.:
- who wrote this code?

OK
That would be all.
Regards and happy coding.
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
511 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Tab Style Links in Header
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Vertically Expanding a DIV inside a Table TD





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC