Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-26-2004, 11:01 PM   PM User | #1
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
getElementById returning no length for existing id

Is there something wrong with my script? Typo?

Code:
function accite() {
	bq = document.getElementById("content").getElementsByTagName("blockquote");
	q = document.getElementById("content").getElementsByTagName("q");
	alert(
		"<blockquote /> = " + bq.length +
		"<q /> = " + q.length
	);
}
It's saying "document.getElementById('content') has no properties", and yes, I definitely have a <div id="content" />.
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 11-26-2004, 11:04 PM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Show us more of your source. Specifically, show us the HTML in question.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 11-26-2004, 11:44 PM   PM User | #3
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
Here is an example page: http://sethrasmussen.com/index.php?p=86

I should note that I am having the problem in Firefox and IE. It actually seems to work fine in Opera, though. Dunno about more yet.
__________________

Opposite of Sequitur

Last edited by ]|V|[agnus; 11-26-2004 at 11:48 PM..
]|V|[agnus is offline   Reply With Quote
Old 11-26-2004, 11:59 PM   PM User | #4
hemebond
Senior Coder

 
Join Date: Jul 2004
Location: New Zealand
Posts: 1,315
Thanks: 0
Thanked 2 Times in 2 Posts
hemebond is an unknown quantity at this point
If you only have one instance of the blockquote or q elements, getElementsByTagName will return that object instead of an array of objects.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<title>48059</title>
	</head>
	<body>
		<div id="content">
			<blockquote></blockquote>
			<q></q>
		</div>

		<script type="text/javascript">
			var bq = document.getElementById("content").getElementsByTagName("blockquote");
			if(bq.length == undefined)
			{
				bq = new Array(bq);
			}

			var q = document.getElementById("content").getElementsByTagName("q");
			if(q.length == undefined)
			{
				q = new Array(q);
			}

			alert(
				"<blockquote /> = " + bq.length +
				"<q /> = " + q.length
			);
		</script>
	</body>
</html>
hemebond is offline   Reply With Quote
Old 11-27-2004, 12:00 AM   PM User | #5
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
Well, okay, but the problem is that IE and Firefox are choking before they even get to that. They bomb on "getElementById('content')"
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 11-27-2004, 12:02 AM   PM User | #6
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
For one, your calling the function window.onload is premature and the content division has not yet been written to the page... Sans the error... Try body.onload instead...

Code:
function accite() {
	bq = document.getElementById("content").getElementsByTagName("blockquote");
	q = document.getElementById("content").getElementsByTagName("q");
	alert(
		"<blockquote /> = " + bq.length +
		"<q /> = " + q.length
	);
}

// init
function init () {accite();}
window.onload=init();
.....Willy
Willy Duitt is offline   Reply With Quote
Old 11-27-2004, 12:05 AM   PM User | #7
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
"body is not defined"
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 11-27-2004, 01:11 AM   PM User | #8
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
You can't change window.onload = init() with body.onload = init() in the HEAD because well.... the body has not been written to the page yet...

Try: <body onload="init()">
Willy Duitt is offline   Reply With Quote
Old 11-27-2004, 01:44 AM   PM User | #9
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
very well, but how can i avoid adding the event handler to the structure?

document.getElementsByTagName("body").onload=init(); doesn't seem to work.

perhaps, document.getElementById("sethrasmussen-dot-com").onload=init(); ?

assuming of course that's the name of the body element?
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 11-27-2004, 01:45 AM   PM User | #10
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
no no that doesn't work... perhaps i include a file at the end of every page with sort of _exit scripts?
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 11-27-2004, 02:06 AM   PM User | #11
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
Quote:
Originally Posted by ]|V|[agnus
very well, but how can i avoid adding the event handler to the structure?

document.getElementsByTagName("body").onload=init(); doesn't seem to work.

perhaps, document.getElementById("sethrasmussen-dot-com").onload=init(); ?

assuming of course that's the name of the body element?
getElementsByTagName returns an array....
Try: document.getElementsByTagName("body")[0].onload=function(){init()};
Willy Duitt is offline   Reply With Quote
Old 11-27-2004, 03:06 AM   PM User | #12
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
well i'm just doing the onload attribute for now. what i'm trying to do with my script is to insert links after blockquotes and quotes that have their href's generated by the blockquote or quote's cite attributes. i'm getting no errors with the following, but no links are inserted:

Code:
function accite() {
	d = document;
	bq = d.getElementById("content").getElementsByTagName("blockquote");
	q = d.getElementById("content").getElementsByTagName("q");
	// cite blockquotes
	for(i=0;i<bq.length;i++){
		newP=d.createElement("p");
		newA=d.createElement("a");
		newA.setAttribute("href",bq[i].getAttribute("cite"));
		newP.appendChild(newA);
		bq[i].appendChild(newP);
	}
}
that code is being run on the same link from above.
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 11-27-2004, 03:02 PM   PM User | #13
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
Although you are now calling the accite function body.onload.... You are still using window.onload to call the init function which in turn calls the accite and thus is still throwing the aforementioned error...

This could be causing a conflict but is hard for me to tell since your entire page does not work for me using IE6... There is much text overlayed upon other text, I can not scroll, view source does not open notepad although the context menu is there and the page seems to take a lot of cpu to display....

Anyways, it is hard for me to see what is going on or what it is supposed to look like...

.....Willy
Willy Duitt is offline   Reply With Quote
Old 11-27-2004, 05:03 PM   PM User | #14
]|V|[agnus
Regular Coder

 
Join Date: May 2004
Location: Minneapolis, MN, USA
Posts: 904
Thanks: 0
Thanked 0 Times in 0 Posts
]|V|[agnus is an unknown quantity at this point
Quote:
Originally Posted by Willy Duitt
Although you are now calling the accite function body.onload.... You are still using window.onload to call the init function which in turn calls the accite and thus is still throwing the aforementioned error...
No, I'm not.

Quote:
Originally Posted by Willy Duitt
This could be causing a conflict
What conflict? I am not getting any errors anymore, but the desired effect is not happening. I think it's because I am trying to use appendChild on a new element that has not been added to the tree yet itself. Yes?

Quote:
Originally Posted by Willy Duitt
but is hard for me to tell since your entire page does not work for me using IE6... There is much text overlayed upon other text, I can not scroll, view source does not open notepad although the context menu is there and the page seems to take a lot of cpu to display....

Anyways, it is hard for me to see what is going on or what it is supposed to look like...
...



Quote:
Originally Posted by sethrasmussen.com/in/colophon.php
That said, users of Internet Explorer can expect persistent bugginess, as this site makes use of modern techniques, the bulk of which IE only partially supports. Despite its massive market share, I've decided not to go out of my way to accomodate this archaic browser since the rest of the competition has continued to innovate and move forward. This site is about progress, not market share.
__________________

Opposite of Sequitur
]|V|[agnus is offline   Reply With Quote
Old 11-27-2004, 05:20 PM   PM User | #15
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
If you are no longer calling the init function window.onload you changed it since I last looked at it shortly after you posted "that code is being run on the same link from above".... the window.onload = init() is in your external javascript file...

And FWIW: What you call modern browsers I call equally buggy if not moreso than IE... Heck, if Firefox is so wonderful why did this latest release break nearly every plugin that was made in the past? Now, all those plugins need to be patched... How long can we expect that to continue?

Besides, IE is one of the most forgiving browsers there is... If your code completely breaks using IE, there is something wrong and the quick look I took at your code shows no indication of cutting-edge code writing....

That being said, I was only trying to help you by pointing out some obvious mistakes... If you have removed the window.onload from the external file, great for you but roll your eyes at someone else....

.....Willy
Willy Duitt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:07 AM.


Advertisement
Log in to turn off these ads.