HighDots Forums  

default behavior <a> and return true|false

Javascript JavaScript language (comp.lang.javascript)


Discuss default behavior <a> and return true|false in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mk834tt@yahoo.com
 
Posts: n/a

Default default behavior <a> and return true|false - 12-15-2007 , 03:41 PM







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[i].getAttribute("class") == "popup") {
alert("is pop up type class");
lnks[i].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


Reply With Quote
  #2  
Old   
mk834tt@yahoo.com
 
Posts: n/a

Default Re: default behavior <a> and return true|false - 12-15-2007 , 05:11 PM







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

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


Reply With Quote
  #3  
Old   
David Mark
 
Posts: n/a

Default Re: default behavior <a> and return true|false - 12-15-2007 , 07:12 PM



On Dec 15, 4:41 pm, mk83... (AT) yahoo (DOT) com wrote:
Quote:
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.

Quote:
window.onload = function() {
if (!document.getElementsByTagName) return false;
var lnks = document.getElementsByTagName("a");
for (var i=0; i<lnks.length; i++) {
if (lnks[i].getAttribute("class") == "popup") {
Oops. MS botched getAttribute. Won't work for "class." Use the
className property instead.

Quote:
alert("is pop up type class");
lnks[i].onclick = function x() {
popUp(this.getAttribute("href"));
return false;
This is a mistake too. The window might not open.

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

Quote:
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.


Reply With Quote
  #4  
Old   
mk834tt@yahoo.com
 
Posts: n/a

Default Re: default behavior <a> and return true|false - 12-15-2007 , 08:02 PM



On Dec 15, 7:12 pm, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote:
Quote:
On Dec 15, 4:41 pm, mk83... (AT) yahoo (DOT) com wrote:


window.onload = function() {
if (!document.getElementsByTagName) return false;
var lnks = document.getElementsByTagName("a");
for (var i=0; i<lnks.length; i++) {
if (lnks[i].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!

Quote:
alert("is pop up type class");
lnks[i].onclick = function x() {
popUp(this.getAttribute("href"));
return false;

This is a mistake too. The window might not open.
Why, is getAttribute("href") wrong?


Quote:
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.


Reply With Quote
  #5  
Old   
Randy Webb
 
Posts: n/a

Default Re: default behavior <a> and return true|false - 12-15-2007 , 10:23 PM



mk834tt (AT) yahoo (DOT) com said the following on 12/15/2007 9:02 PM:
Quote:
On Dec 15, 7:12 pm, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote:
On Dec 15, 4:41 pm, mk83... (AT) yahoo (DOT) com wrote:


window.onload = function() {
if (!document.getElementsByTagName) return false;
var lnks = document.getElementsByTagName("a");
for (var i=0; i<lnks.length; i++) {
if (lnks[i].getAttribute("class") == "popup") {
Oops. MS botched getAttribute. Won't work for "class." Use the
className property instead.

What?, getAttribute("class") changes between browsers.
if(lnks[i].className == "popup"){

Now, it is cross-browser.

Quote:
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!
No, you test the property directly and forget about getAttribute.

Quote:
alert("is pop up type class");
lnks[i].onclick = function x() {
popUp(this.getAttribute("href"));
return false;
This is a mistake too. The window might not open.
Why, is getAttribute("href") wrong?
That isn't what David was referring to. If the user has popups disabled,
then the user will never see your link.

As for getAttribute('href'), it is "safer" to access the property directly:

popup(this.href);

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


Reply With Quote
  #6  
Old   
Steve Swift
 
Posts: n/a

Default Re: default behavior <a> and return true|false - 12-16-2007 , 12:58 AM



mk834tt (AT) yahoo (DOT) com wrote:
Quote:
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. :-)

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk


Reply With Quote
  #7  
Old   
mk834tt@yahoo.com
 
Posts: n/a

Default Re: default behavior <a> and return true|false - 12-16-2007 , 09:02 AM



On Dec 16, 12:58 am, Steve Swift <Steve.J.Sw... (AT) gmail (DOT) com> wrote:
Quote:
mk83... (AT) yahoo (DOT) com 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. :-)

--
Steve Swifthttp://www.swiftys.org.uk/swifty.htmlhttp://www.ringers.org.uk
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.


Reply With Quote
  #8  
Old   
Randy Webb
 
Posts: n/a

Default Re: default behavior <a> and return true|false - 12-16-2007 , 12:01 PM



mk834tt (AT) yahoo (DOT) com said the following on 12/16/2007 10:02 AM:
Quote:
On Dec 16, 12:58 am, Steve Swift <Steve.J.Sw... (AT) gmail (DOT) com> wrote:
mk83... (AT) yahoo (DOT) com 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.
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/


Reply With Quote
  #9  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: default behavior <a> and return true|false - 12-16-2007 , 12:01 PM



mk834tt (AT) yahoo (DOT) com wrote:
Quote:
On Dec 15, 7:12 pm, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote:
On Dec 15, 4:41 pm, mk83... (AT) yahoo (DOT) com wrote:
for (var i=0; i<lnks.length; i++) {
if (lnks[i].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[i]' 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
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>


Reply With Quote
  #10  
Old   
David Mark
 
Posts: n/a

Default Re: default behavior <a> and return true|false - 12-16-2007 , 01:14 PM



On Dec 15, 9:02 pm, mk83... (AT) yahoo (DOT) com wrote:
Quote:
On Dec 15, 7:12 pm, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote:

On Dec 15, 4:41 pm, mk83... (AT) yahoo (DOT) com wrote:

window.onload = function() {
if (!document.getElementsByTagName) return false;
var lnks = document.getElementsByTagName("a");
for (var i=0; i<lnks.length; i++) {
if (lnks[i].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.

Quote:
alert("is pop up type class");
lnks[i].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.

Quote:
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?
You created a closure with a circular reference to a DOM object. See:

http://www.jibbering.com/faq/faq_notes/closures.html

Quote:
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.

Quote:
during the browser session, or does it just lay there after all
browser sessions are closed.
Closing the browser will restore the leaked memory.

Quote:
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.

Quote:
Thank you very much.
You are welcome.


Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.