472,353 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Cross browser DHTML, ala quirksmode

Hey all:

I'm stuck. I'm using the code from www.quirksmode.org for cross
browser access to HTML elements, but I'm getting a javascript error.
The relevant code:

function getObj(name)
if (document.getElementById)
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style;
else if (document.all)
this.obj = document.all[name];
this.style = document.all[name].style;
else if (document.layers)
this.obj = document.layers[name];
this.style = document.layers[name];

OK -- this is straight from quirksmode, and I have seen the same or
similar code in just about every javascript library. I have the
following HTML:

<img id="right_image" src="data/1.gif" alt="right image" />

And the following css:

position: absolute;

So, when I call:

var rightImage = new getObj('right_image');

I get an error:

"Error: document.getElementById(name) has no properties"

However, if I comment out "this.style =
document.getElementById(name).style;" from the getObj function, the
error goes away.

So, what am I doing wrong?


Jul 23 '05 #1
3 1331
cjl wrote:
"Error: document.getElementById(name) has no properties" However, if I comment out "this.style =
document.getElementById(name).style;" from the getObj function, the
error goes away.

Try alert(document.getElementById(name)), you'll see it's undefined,
which is why looking up the style property fails miserably. To solve
this, what about calling the function *after* the HTML element is
parsed... ;-)
Jul 23 '05 #2
"Error: document.getElementById(name) has no properties"
This happens when the browser has yet to render that element in the web
page, or when such element with the given Id does not exist. But in
your case, it might be more probable of the first scenario.

A second item to address (this is just me being picky). On the lines
in which you do the following:
this.style = document.getElementById(name).*style;
this.style = document.all[name].style;
this.style = document.layers[name];

You can speed up execution time by reducing DOM lookups by doing the

this.style = this.obj.style;

:) If you were able to grab the object in the previous line, then you
don't need to grab the object again to reference its style, when you
could just refer to it directly.

Hope this helps.

Jul 23 '05 #3
Hey all:

Once again, thank you for the quick and helpful replies. This
newsgroup is amazing!

I was making the mistake that both of you assumed, the javascript was
called before the HTML had been parsed. This has been fixed, and now
everything is working.

web.dev, I swiped the code wholesale from quirksmode, but I see your
point, and will make the suggested changes.


Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: muraii | last post by:
Hi all, I've trolled through Google, a little of evolt, css-discuss archives, and sent the list a request, all to no avail. I consulted...
by: Questman | last post by:
Good afternoon, Does anyone have any code that implements, or approaches implementing, a cross-browser DHTML/JS solution to provide an Excel-like...
by: CMM | last post by:
So I'm half way through overseeing a large project in ASP.NET 2.0. My superiors have decided that it would be nice if we ensured the site worked on...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.