Current location: Hot Scripts Forums » General Web Coding » JavaScript » Internet Explorer problem?


Internet Explorer problem?

Reply
  #1 (permalink)  
Old 05-12-08, 05:28 AM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
Internet Explorer problem?

I use the following code to show all elements in alert box after page is loaded:

javascript Code:
  1. var items = document.getElementsByTagName("*");
  2.        
  3. for (var i=0; i < items.length; i++)
  4. {
  5.     alert(items[i].nodeName);
  6. }

My code work in Firefox 2.x, Opera and Safari but internet explorer not show the alert box. I use IE 7.
I read somewhere, I need to use "document.all" instead of "document.getElementsByTagName("*")" for explorer but I have the same problem.

Any idea? thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-12-08, 08:01 AM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
I don't think that * is a valid tag. Strange that it works in FF and such.
Your code looks ok to me, the only thing that can cause the problem is your document.getElementsByTagName("*") call.

Since you seem to be in a learning mood, I can tell you that your loop is quite "slow". The items.length call is expensive timewise. And you make that call in every loop iteration. The fastest way would be:
javascript Code:
  1. for (var i=0,l=items.length;i < l; i++){
  2. alert(items[i].nodeName);
  3. }

Before alerting the nodeName, you should check the nodeType. Because IE will return a line break in your code as a text-node. This can cause you errors.
__________________
Jack Bauer makes Chuck Norris cry
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-12-08, 04:13 PM
zoliky's Avatar
zoliky zoliky is offline
Aspiring Coder
 
Join Date: Jun 2006
Posts: 537
Thanks: 0
Thanked 0 Times in 0 Posts
I don't know if asterisk (*) is valid but my Javascript book teach this way.

First of all, we’ll need to get all the elements in the document. We do this using getElementsByTagName, but we’re not going to look for a particular tag; instead, we’re going to pass this method the special value "*", which tells it to return all elements.

Anyway I have another question related to getElementsByTagName.

For example this HTML CODE:

Code:
<html>
<head>
  <title>My title</title>
</head>
<body>
   <ul class="my class">
     <li>one</li>
     <li>two</li>
   </ul>
</body>
</html>
and this javascript code:

javascript Code:
  1. var mynode = document.getElementsByTagName("ul");

The question:
The Javascript code get only the element name, for example : UL or get the element name and the class name ?

Last edited by zoliky; 05-12-08 at 04:24 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-13-08, 03:33 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
to access the attributes (such as "class", "style", "id", ....) of a node, you have to use the attributes property of the node:
JavaScript Code:
  1. var mynode = document.getElementsByTagName ("*");
  2. for (var i in mynode.attributes)
  3.   alert (i + "=" + mynode.attributes[i]);
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] A problem with internet explorer...again sushi4664 HTML/XHTML/XML 7 05-04-08 03:06 AM
Broswer Compatability involving a WYSIWYG Editor NomadMan JavaScript 1 08-18-07 04:20 PM
Problem with PHP/.htaccess script, when using Internet Explorer digioz PHP 7 07-27-07 12:17 AM
Internet explorer problem sharad JavaScript 1 01-07-05 09:57 PM
Asp.net web controls are not showing in internet explorer shawais ASP.NET 3 05-04-04 06:16 AM


All times are GMT -5. The time now is 07:26 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.