473,480 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Ghost in the Machine or "Is my HTML somehow creating javascript objects???"

I have quite an odd question. Usually I ask whenever javascript is not
acting the way I'd expect. This time I'm trying to figure out why
something works when I don't think it should be working!

I'm working on a web page that has a <div> area for dynamic content
display (id='content').

In between the </body> and </html> tags is where I'm storing the text
to be displayed when needed. They are stored inside wrapper <div>s
which are set to "display:none" (<div id='wrap1' class='cloak'> etc.)

I change the content displayed thusly:
onclick="content.innerHTML=wrap1.innerHTML"

I don't understand why this works, because *nowhere* in my document
have I declared:
var content=document.getElementById('content')

I *should* be using:

onclick="getElementById('content').innerHTML=getEl ementById('wrap1').innerHTML"

Shouldn't I? In fact, that is the way I originally started doing it.
Then I started using variables so that I could remove some of the
"getElementById"s and by fluke I discovered it wasn't necessary to set
the variables. I've even rebooted in case there was some memory leak
keeping those variables set even after I've removed their declaration
from my script, but it still continues to work. Why is it working? Is
this a hidden feature of IE? Or have I missed something obvious?

Lance

Jul 23 '05 #1
3 1309
Lance wrote:
[snip]

I change the content displayed thusly:
onclick="content.innerHTML=wrap1.innerHTML"

I don't understand why this works, because *nowhere* in my document
have I declared:
var content=document.getElementById('content')

I *should* be using:

onclick="getElementById('content').innerHTML=getEl ementById('wrap1').innerHTML"
[...]
Why is it working? Is
this a hidden feature of IE?


Well, not hidden, and IMO, not a feature.

Mck
Jul 23 '05 #2
On 09/07/2005 22:43, Lance wrote:

[snip]
I'm working on a web page that has a <div> area for dynamic content
display (id='content').

In between the </body> and </html> tags is where I'm storing the text
to be displayed when needed.
Why? Only the HEAD and BODY elements are valid children of the HTML element.
They are stored inside wrapper <div>s which are set to "display:none"
(<div id='wrap1' class='cloak'> etc.)
Why don't you place them where the 'content' DIV is and toggle the
display property? It should be more efficient than having the browser
parse HTML and build a document fragment, it's valid, and will degrade
better if CSS or client-side scripting is unavailable or disabled.
I change the content displayed thusly:
onclick="content.innerHTML=wrap1.innerHTML"

I don't understand why this works [...]
In some browsers (most notably IE), elements that possess name or id
attributes are made properties of other objects. In this case, an
element with an id attribute is available as a property of the global
object.

[snip]
I *should* be using:

onclick="getElementById('content').innerHTML=getEl ementById('wrap1').innerHTML"
No, you should probably be doing what I suggested above, but if you
don't like that idea, then the fragment above is still unwise, too. The
getElementById method is a property of the document object, and there is
no reason to assume that the document object will automatically be part
of the scope chain used to resolve identifiers. The code above is still
likely to fail on many browsers. Prefix the function call with 'document.'.

[snip]
Why is it working? Is this a hidden feature of IE? [...]


It's not a hidden feature, but it's a bad one in my opinion. Don't use it.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 23 '05 #3
VK
> I change the content displayed thusly:
onclick="content.innerHTML=wra*p1.innerHTML"
I don't understand why this works, because *nowhere* in my document
have I declared:
var content=document.getElementByI*d('content')


It's so called "default member", something Microsoft likes to use a lot
in their languages.

content.innerHTML=wra*p1.innerHTML;

effectively equals to:

with (window) {
content.innerHTML=wra*p1.innerHTML;
}

It's not as bad as:

<form name="myForm">
....
<input type="submit" name="submit" value="Submit">
</form>

Now try to submit the form using document.forms[0].submit() and be
amased.

(All this crap is IE-only I guess)

Jul 23 '05 #4

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

Similar topics

1
1470
by: Daniel | last post by:
Is there any way for System.IO.StreamWriter Write method to write out part of the string to file. such as if the machine is shut down half way through? or does the file not actually exist until the...
21
3318
by: Evan Sussman | last post by:
Could any one give me an explenation of this error? I get it every once in a while, and it really is a pain. currently the code that I'm working with goes like this <script> <!-- var...
5
2108
by: Kevin Buchan | last post by:
How can I tell if the system is 'idle', or the screen saver is running, or the machine is locked? I am writing an application that could really benefit from knowing this information. During...
21
13757
by: codergem | last post by:
One common answer is that all compilers keep the size of integer the same as the size of the register on a particular architecture. Thus, to know whether the machine is 32 bit or 64 bit, just see...
2
4364
by: hardrock | last post by:
Hello! I'm working with the prototype library version 1.4.0 and having a strange error lately. When I want to make an Ajax.Updater call, it basically works. But as soon as I put the call into...
3
4119
by: jason | last post by:
I've been playing around with new (for 2.0) membershp functionality. I was able to build a simple login form that secures a directory on a project I built locally on my development desktop. ...
37
3889
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
3
4285
by: blackrunner | last post by:
ERROR in my Query?! ERROR: Element GESCHLECHT is undefined in FORM. i think everything ok. Maby somebody can help me here Element GESCHLECHT is undefined in FORM. The error occurred...
6
4935
by: cleary1981 | last post by:
I have adapted code from http://dunnbypaul.net/js_mouse/ I want to use a button to create new draggable divs but i keep getting error "is null or not an object" heres the code <html> <head>...
0
7049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6912
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
7092
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...
1
6744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6981
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3000
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.