Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > default behavior <a> and return true|false

Reply
Thread Tools

default behavior <a> and return true|false

 
 
mk834tt@yahoo.com
Guest
Posts: n/a
 
      12-15-2007

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
 
 
 
 
mk834tt@yahoo.com
Guest
Posts: n/a
 
      12-15-2007

> 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
 
 
 
 
David Mark
Guest
Posts: n/a
 
      12-16-2007
On Dec 15, 4:41 pm, mk83...@yahoo.com wrote:
> 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[i].getAttribute("class") == "popup") {


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

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

>
> } } } }


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.
 
Reply With Quote
 
mk834tt@yahoo.com
Guest
Posts: n/a
 
      12-16-2007
On Dec 15, 7:12 pm, David Mark <dmark.cins...@gmail.com> wrote:
> On Dec 15, 4:41 pm, mk83...@yahoo.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!

> > 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?


> 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
 
Steve Swift
Guest
Posts: n/a
 
      12-16-2007
wrote:
> 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
 
mk834tt@yahoo.com
Guest
Posts: n/a
 
      12-16-2007
On Dec 16, 12:58 am, Steve Swift <Steve.J.Sw...@gmail.com> wrote:
> mk83...@yahoo.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
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      12-16-2007
wrote:
> On Dec 15, 7:12 pm, David Mark <dmark.cins...@gmail.com> wrote:
>> On Dec 15, 4:41 pm, mk83...@yahoo.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
 
David Mark
Guest
Posts: n/a
 
      12-16-2007
On Dec 15, 9:02 pm, mk83...@yahoo.com wrote:
> On Dec 15, 7:12 pm, David Mark <dmark.cins...@gmail.com> wrote:
>
> > On Dec 15, 4:41 pm, mk83...@yahoo.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.

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

>
> > 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

>
> 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.
 
Reply With Quote
 
2apart
Guest
Posts: n/a
 
      12-16-2007

> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASPNET 2.0 Application exhibits strange behavior. Method calls return "False" until we recycle application pool. Dan D. ASP .Net 0 09-12-2006 02:33 PM
C++ compiler "return" behavior (guru question ;) Axel Bock C++ 32 04-14-2006 11:57 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
controlling "return key" behavior with multiple "form-elements" Holger (David) Wagner ASP .Net 1 08-17-2004 08:08 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57