explain what this means

W

windandwaves

Hi Folk

I found the following function somewhere on the web. I am not sure how it
works, but I am sure that it is very useful:

function getObj(name) {
if (document.getElementById){
if(document.getElementById(name)) {
if(document.getElementById(name).style) {
this.obj document.getElementById(name);
this.style document.getElementById(name);
}
}
}
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];
}
}

I guess that it finds out how the browser refers to elements with a specific
IDs and then returns them. However, I dont really understand "this." other
than that it refers to the function itself and how it or what it returns.
can someone explain it?

TIA

- Nicolaas
 
R

RobG

windandwaves said:
Hi Folk

I found the following function somewhere on the web. I am not sure how it
works, but I am sure that it is very useful:

It may be useful for particular circumstances related to legacy code and
support for old browsers. It is likely that better methods exist for
new applications.

Read the FAQ and look for DynWrite.
function getObj(name) {
if (document.getElementById){
if(document.getElementById(name)) {
if(document.getElementById(name).style) {
this.obj document.getElementById(name);
this.style document.getElementById(name);

The last two lines should have an equals sign '=' and the last one
probably should be a reference to the element's style object:

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];
}
}

I guess that it finds out how the browser refers to elements with a specific
IDs and then returns them.

The function doesn't return anything.
However, I dont really understand "this." other
than that it refers to the function itself and how it or what it returns.
can someone explain it?

Read this bit of quirksmode (and anything else that takes your fancy,
it's all good):

<URL:http://www.quirksmode.org/js/this.html>
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 24
Aug 2005 17:25:01, seen in windandwaves
I found the following function somewhere on the web. I am not sure how it
works, but I am sure that it is very useful:

function getObj(name) {
if (document.getElementById){
if(document.getElementById(name)) {
if(document.getElementById(name).style) {
this.obj document.getElementById(name);
this.style document.getElementById(name);
}
}
}

It looks rather inefficient, as it does the search for name four
times. After removing the actual errors, consider something (untested)
along the lines of

function getObj(name) { var T
if (T = document.getElementById){
if (T = T(name)) {
if (T.style) {
this.obj T;
this.style T;
}
}
}
 
W

windandwaves

windandwaves wrote:
Thank you all for your comments. Much appreciated. I wrote this function:

function getObj (name) {
var T;
if (document.getElementById){
T = document.getElementById(name);
if(T) {
if(T.style) {
return T;
}
}
}
}

Which seems to serve my needs: return something if the element can be found
and the style can be changed.

Thank you again.

- Nicolaas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top