default behavior <a> and return true|false

Discussion in 'Javascript' started by mk834tt@yahoo.com, Dec 15, 2007.

  1. Guest

    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
    , Dec 15, 2007
    #1
    1. Advertising

  2. Guest


    > 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.
    , Dec 15, 2007
    #2
    1. Advertising

  3. David Mark Guest

    On Dec 15, 4:41 pm, 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.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.
    David Mark, Dec 16, 2007
    #3
  4. Guest

    On Dec 15, 7:12 pm, David Mark <> wrote:
    > On Dec 15, 4:41 pm, 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.
    , Dec 16, 2007
    #4
  5. Steve Swift Guest

    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
    Steve Swift, Dec 16, 2007
    #5
  6. Guest

    On Dec 16, 12:58 am, Steve Swift <> wrote:
    > 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.
    , Dec 16, 2007
    #6
  7. wrote:
    > On Dec 15, 7:12 pm, David Mark <> wrote:
    >> On Dec 15, 4:41 pm, wrote:
    >>> 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
    --
    "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>
    Thomas 'PointedEars' Lahn, Dec 16, 2007
    #7
  8. David Mark Guest

    On Dec 15, 9:02 pm, wrote:
    > On Dec 15, 7:12 pm, David Mark <> wrote:
    >
    > > On Dec 15, 4:41 pm, 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.

    >
    > > 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.
    David Mark, Dec 16, 2007
    #8
  9. 2apart Guest


    > 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? :)
    2apart, Dec 16, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    652
    Chris Theis
    Jun 18, 2004
  2. Pierre Quentel

    "0 in [True,False]" returns True

    Pierre Quentel, Dec 12, 2005, in forum: Python
    Replies:
    59
    Views:
    1,020
    Grant Edwards
    Dec 16, 2005
  3. André
    Replies:
    3
    Views:
    1,560
  4. bdb112
    Replies:
    45
    Views:
    1,319
    jazbees
    Apr 29, 2009
  5. w i l l
    Replies:
    4
    Views:
    230
    Dan Brussee
    Jul 4, 2003
Loading...

Share This Page