473,396 Members | 2,111 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,396 software developers and data experts.

Cross browser DHTML, ala quirksmode

cjl
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:

#right_image
{
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?

-CJL

Jul 23 '05 #1
3 1372
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
Hi CJL,
"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
following:

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
cjl
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.

-CJL

Jul 23 '05 #4

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

Similar topics

11
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 positioniseverything.net and quirksmode.org. The issue I...
2
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 Grid on a web page - I'm trying to convert an...
15
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 all the major platforms (as they see it: IE,...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.