default behavior <a> and return true|false

M

mk834tt

This is an example in "DOM Scripting". It works. I don't understand
why.

window.onload = function() {
if (!document.getElementsByTagName) return false;
var lnks = document.getElementsByTagName("a");
for (var i=0; i<lnks.length; i++) {
if (lnks.getAttribute("class") == "popup") {
alert("is pop up type class");
lnks.onclick = function x() {
popUp(this.getAttribute("href"));
return false;
} } } }

And here is the anchor

<a href="http://www.dogpile.com" class="popup" >POPUP</a><br/>

To create his "unobtrusive" javascript, there is a test for existence
of getElementByTagName. He returns false if it does not exist. I
thought false precluded the default behavior or the <a> element.
Shouldn't the test return true if the object is not found so that the
page can load dogpile?

Thanks
 
M

mk834tt

window.onload = function() {
if (!document.getElementsByTagName) return false;
var lnks = document.getElementsByTagName("a");
for (var i=0; i<lnks.length; i++) {
if (lnks.getAttribute("class") == "popup") {
alert("is pop up type class");
lnks.onclick = function x() {
popUp(this.getAttribute("href"));
return false;



Never mind. I got it. Never changes any element attribute.
 
D

David Mark

This is an example in "DOM Scripting". It works. I don't understand
why.

It is a very bad example of "DOM Scripting" and certainly will not
work in IE. At least IE users won't have to put up with popup windows
it attempts to create.
window.onload = function() {
if (!document.getElementsByTagName) return false;
var lnks = document.getElementsByTagName("a");
for (var i=0; i<lnks.length; i++) {
if (lnks.getAttribute("class") == "popup") {


Oops. MS botched getAttribute. Won't work for "class." Use the
className property instead.
alert("is pop up type class");
lnks.onclick = function x() {
popUp(this.getAttribute("href"));
return false;


This is a mistake too. The window might not open.

Forgot to set lnks to null as well. This will leak memory in IE.
And here is the anchor

<a href="http://www.dogpile.com" class="popup" >POPUP</a><br/>

To create his "unobtrusive" javascript, there is a test for existence
of getElementByTagName. He returns false if it does not exist. I
thought false precluded the default behavior or the <a> element.
Shouldn't the test return true if the object is not found so that the
page can load dogpile?

Returning false from the load listener has nothing to do with it. The
click listeners were not attached at all in that case.
 
M

mk834tt

On Dec 15, 4:41 pm, (e-mail address removed) wrote:
window.onload = function() {
if (!document.getElementsByTagName) return false;
var lnks = document.getElementsByTagName("a");
for (var i=0; i<lnks.length; i++) {
if (lnks.getAttribute("class") == "popup") {


Oops. MS botched getAttribute. Won't work for "class." Use the
className property instead.


What?, getAttribute("class") changes between browsers. That's a basic
commonly used function. Do you encapsulate all calls to getAttribute
then,
or do you do tests on both tags, "class" and "className". Good grief!
alert("is pop up type class");
lnks.onclick = function x() {
popUp(this.getAttribute("href"));
return false;


This is a mistake too. The window might not open.

Why, is getAttribute("href") wrong?

Forgot to set lnks to null as well. This will leak memory in IE.

I have to take care of destroying objects? I thought 'var
this_or_that' was a local var. No?

How would you know if there was a memory leak? Does is persist only
during the browser session, or does it just lay there after all
browser sessions are closed.

This is going to be harder than I thought. I suppose a developer
should have a copy of the popular browsers and test each page.

Thank you very much.
 
S

Steve Swift

This is an example in "DOM Scripting". It works. I don't understand
why.
...

Five hours and eleven minutes previously (according to my view of the
world) you declared yourself a beginner. In the intervening time you've
learned enough javascript to utterly confuse me, and I've been
struggling with it for over ten years. I'd like to know what you're
learning from. :)
 
M

mk834tt

(e-mail address removed) wrote:
Five hours and eleven minutes previously (according to my view of the
world) you declared yourself a beginner. In the intervening time you've
learned enough javascript to utterly confuse me, and I've been
struggling with it for over ten years. I'd like to know what you're
learning from. :)

Just two main places (and now here) Started here "http://
www.w3schools.com/default.asp" and purchased a book titled "DOM
Scripting" by Jeremy Keith. Good book. Have to confess I do know a
little perl and java too. The java REALLY helped me spin up
javascript. It's the javascript and HTML DOM "libraries" (objects-
models-how to use) that really has me flustered.
 
T

Thomas 'PointedEars' Lahn

for (var i=0; i<lnks.length; i++) {
if (lnks.getAttribute("class") == "popup") {

Oops. MS botched getAttribute. Won't work for "class." Use the
className property instead.


What?, getAttribute("class") changes between browsers. That's a basic
commonly used function.


No, it is not, at least not among developers with a minimum clue.

First, it is a _method_. As it is a host object's method it is not
necessarily available as a native Function object.

Second, HTML element objects have attribute-value properties that should be
used where possible instead of calling getAttribute(). In this case, the
`className' attribute of the HTMLElement interface, and so the `className'
property of the object referred to by `lnks' provides access to the
represented element's `class' attribute value. (`class' as interface
attribute/object property name was not available as `class' is a reserved
word in the languages for which binding is defined.)

Third, getAttribute() returns the *attribute* value. That is not (always)
identical with the current value of the *element object*, as it is the case
for form controls.

http://www.w3.org/TR/DOM-Level-2-HTML/
(Proprietary DOMs implement these interfaces or define attribute-value
properties themselves.)


PointedEars
 
D

David Mark

On Dec 15, 4:41 pm, (e-mail address removed) wrote:
window.onload = function() {
if (!document.getElementsByTagName) return false;
var lnks = document.getElementsByTagName("a");
for (var i=0; i<lnks.length; i++) {
if (lnks.getAttribute("class") == "popup") {

Oops. MS botched getAttribute. Won't work for "class." Use the
className property instead.

What?, getAttribute("class") changes between browsers. That's a basic
commonly used function. Do you encapsulate all calls to getAttribute
then,
or do you do tests on both tags, "class" and "className". Good grief!


In most cases you don't need to use getAttribute, so it is rarely an
issue.
alert("is pop up type class");
lnks.onclick = function x() {
popUp(this.getAttribute("href"));
return false;

This is a mistake too. The window might not open.

Why, is getAttribute("href") wrong?


As already pointed out, it is the following line that is wrong. You
are blindly returning false, without making any effort to test if the
window opened or not.
I have to take care of destroying objects? I thought 'var
this_or_that' was a local var. No?

You created a closure with a circular reference to a DOM object. See:

http://www.jibbering.com/faq/faq_notes/closures.html
How would you know if there was a memory leak? Does is persist only

By understanding the issues involved and occasionally checking Task
Manager in Windows to verify that I didn't forget something.
during the browser session, or does it just lay there after all
browser sessions are closed.

Closing the browser will restore the leaked memory.
This is going to be harder than I thought. I suppose a developer
should have a copy of the popular browsers and test each page.

The first thing a developer should do is learn the basics of
JavaScript and how it interacts with the various DOM implementations.
Testing is a must, but you can't possibly test on every configuration
of every browser. You can't even test on every configuration of the
latest versions of the popular browsers. There are too many variables
involved.
Thank you very much.

You are welcome.
 
2

2apart

I think that is the first time that I have seen someone write "I don't
know anything about javascript but that is a good javascript book". How
do you know it is a good book if you don't know the subject?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

RU serious Randy? First time? :)
 

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