Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getElementById "replacement"

Status
Not open for further replies.

LFI

Programmer
Apr 27, 1999
1,996
US
People,

I just saw this at The grabber states
The document.getElementById method is very useful but isn't supported in early browsers. To define the method for use in those older browsers, just add this snippet to the beginning of your scripts.

I found this snippet interesting because study of it should be helpful to new JavaScript users and even for those of use who use it frequently, but not in the fashion shown in this snippet. Check it out:

Code:
<!-- ONE STEP TO INSTALL GETELEMENTBYID:

  1.  Copy the coding into the HEAD of your HTML document  -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document  -->

<HEAD>

<script type="text/javascript">
<!-- Begin
/* This script and many more are available free online at
The JavaScript Source!! [URL unfurl="true"]http://javascript.internet.com[/URL]
Created by: Ultimater :: [URL unfurl="true"]http://ultimiacian.tripod.com/[/URL]
Add this snippet to the very beginning of your script. */

if(!document.getElementById){
  if(document.all)
  document.getElementById=function(){
    if(typeof document.all[arguments[0]]!="undefined")
    return document.all[arguments[0]]
    else
    return null
  }
  else if(document.layers)
  document.getElementById=function(){
    if(typeof document[arguments[0]]!="undefined")
    return document[arguments[0]]
    else
    return null
  }
}
// End -->
</script>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="[URL unfurl="true"]http://javascriptsource.com">The[/URL] JavaScript Source</a></font>
</center><p>

<!-- Script Size:  0.94 KB -->

The script covers browser compatibility issues with the document.all vs. document.layers if-blocks and seems to suggest that semi-colons aren't always needed.

Anyway, have a nice day!

--Dave

P.S., although I only glance at the newsletter anymore, they have a decent one. You can subscribe to it using the mini-form at the bottom of the page this snippet is on: or via the link I provided up top.
 
Interesting. It could be compacted a little if you wanted:
Code:
<script>
if(!document.getElementById){
  if(document.all)
    document.getElementById=function(id){
      return document.all[id] || null;
    }
  if(document.layers)
    document.getElementById=function(id){
      return document[id] || null;
    }
}
</script>

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top