Closed Bug 363899 Opened 18 years ago Closed 18 years ago

getElementById returns null on empty (no text) pages

Categories

(Firefox :: General, defect)

defect
Not set
minor

Tracking

()

RESOLVED DUPLICATE of bug 178258

People

(Reporter: admin, Unassigned)

Details

(Keywords: testcase)

Attachments

(1 file)

1.05 KB, application/octet-stream
Details
User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.8.1) Gecko/20061010 Firefox/2.0

I have a file called test.html

-----
<a href="http://liczniki.org/" style="border: 0px; padding: 0px; margin: 0px;" id="ministat01aliczniki"></a>
<script type="text/javascript" src="http://127.0.0.90/liczniki/ms.php?l=liczniki" defer="defer">
</script>
-----

now ms.php is a javascript applet that executes

------
[...]
document.getElementById("ministat01aliczniki").appendChild(...)
[...]
------

getElementById returns null if there is no visible text in test.html. To workaround it you must change test.html to something like

------
Some random text so getElementById will work...
<a href="http://liczniki.org/" style="border: 0px; padding: 0px; margin: 0px;" id="ministat01aliczniki"></a>
<script type="text/javascript" src="http://127.0.0.90/liczniki/ms.php?l=liczniki" defer="defer">
</script>
--------

This is rather cosmetic issue than a bug, but web developers may have problems figuring out what is wrong with thier js.

Reproducible: Always

Steps to Reproduce:
0. All files placed on local apache server (dont know if it matters)
1. create test.html, copy from details as descripted (without ---- signs)
2. create ms.php, tell it to execute eg. alert(getElementById('ministat01aliczniki')); ->> run test.html ->> alert displays null pointer
3. enter some text at the beginning of test.html ->> run test.html --> alert displays object

Actual Results:  
getElementById behaviour is inconsistent depending of webpage content

Expected Results:  
getElementById should return object instead of null when web page is empty (no visible text)

Standard install
Does it work if you remove defer="defer" from the script start tag?
Summary: Javascript getElementByID wont work on empty (no text) pages → getElementById returns null on empty (no text) pages
Attached file Examples of that bug
just run 3 html files attached
(In reply to comment #1)
> Does it work if you remove defer="defer" from the script start tag?
> 

no it doesnt work. please look at the attachment for examples
I see what you mean.  This is the same underlying problem as bug 178258,
which is that <script> is a valid <head> element but <a> isn't and is thus
implicitly moved into a auto-created <body>. You can see this in DOM
Inspector (CTRL+SHIFT+I).

I suggest you add an explicit <body> before the <a>. 
That should work in all browsers.

*** This bug has been marked as a duplicate of 178258 ***
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago
Keywords: testcase
OS: Windows XP → All
Hardware: PC → All
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: