Scoping Issue

Discussion in 'Javascript' started by Kevin, Jun 4, 2010.

  1. Kevin

    Kevin Guest

    Hi there,

    My HTML calls two javascript functions:

    1. paint a webpage with textboxes and drop down menus;
    2. populate the webpage with the XML DOM values.

    The paint function, #1, just consists of two functions: drawTextBox
    and drawDropDown menu.

    Inside of these functions I just call document.write(input type=select
    OR type= text ...).

    I need to add onclick functionality to the drop-down menus so that
    when they're clicked, they access a particular URL that the XML DOM
    specifies.

    My problem is that I don't know how to add this onclick functionality.
    If I try to add it in the paint function, I can't because it doesn't
    yet have access to the XML DOM, which is called in function #2. If I
    try to add the onclick functionality using dropdown.onclick = function
    ( window.location=...), I can't get that to work either.

    Please give me advice.

    thank you,
    Kevin
    Kevin, Jun 4, 2010
    #1
    1. Advertising

  2. On 6/3/2010 9:35 PM, Kevin wrote:
    > Hi there,
    >
    > My HTML calls two javascript functions:
    >
    > 1. paint a webpage with textboxes and drop down menus;
    > 2. populate the webpage with the XML DOM values.
    >
    > The paint function, #1, just consists of two functions: drawTextBox
    > and drawDropDown menu.
    >


    Paint? I don't much care for the name of that function, much less what
    it does.

    Putting the HTML in the ousrce code is usually a lot easier to debug and
    run through the validator.

    > Inside of these functions I just call document.write(input type=select
    > OR type= text ...).
    >
    > I need to add onclick functionality to the drop-down menus so that
    > when they're clicked, they access a particular URL that the XML DOM
    > specifies.
    >
    > My problem is that I don't know how to add this onclick functionality.
    > If I try to add it in the paint function, I can't because it doesn't
    > yet have access to the XML DOM, which is called in function #2. If I
    > try to add the onclick functionality using dropdown.onclick = function
    > ( window.location=...), I can't get that to work either.
    >
    > Please give me advice.


    Consider using links for the menu items that need to act like links.

    <a id="CHeader" href="/csection/">
    <ul class="menu" id="CMenu">
    <li><a href="http://example.net/">eggs</a></li>
    </ul>

    Garrett
    Garrett Smith, Jun 4, 2010
    #2
    1. Advertising

  3. Garrett Smith wrote:

    > Kevin wrote:
    >> My HTML calls two javascript functions:
    >>
    >> 1. paint a webpage with textboxes and drop down menus;
    >> 2. populate the webpage with the XML DOM values.
    >>
    >> The paint function, #1, just consists of two functions: drawTextBox
    >> and drawDropDown menu.

    >
    > Paint? I don't much care for the name of that function, much less what
    > it does.


    Indeed, that document would not be accessible. Better to use XSLT
    server-side to transform the XML document into an HTML fragment.

    > Consider using links for the menu items that need to act like links.
    >
    > <a id="CHeader" href="/csection/">


    That element is unfinished, and therefore the resulting markup not Valid.

    > <ul class="menu" id="CMenu">
    > <li><a href="http://example.net/">eggs</a></li>
    > </ul>


    Perhaps you wanted to suggest

    <ul class="menu">
    <li><a href="/csection/">spam</a>
    <ul>
    <li><a href="http://example.net/">eggs</a></li>
    </ul></li>
    </ul>

    Prune unnecessary IDs.


    PointedEars
    --
    Danny Goodman's books are out of date and teach practices that are
    positively harmful for cross-browser scripting.
    -- Richard Cornford, cljs, <cife6q$253$1$> (2004)
    Thomas 'PointedEars' Lahn, Jun 4, 2010
    #3
  4. Kevin

    Kevin Guest


    > Consider using links for the menu items that need to act like links.
    >
    > <a id="CHeader" href="/csection/">
    > <ul class="menu" id="CMenu">
    >    <li><a href="http://example.net/">eggs</a></li>
    > </ul>
    >
    > Garrett


    Hi Garrett,

    I don't see how I can set the "<a href="http://example.net/">"
    statically. The "http://example.net" is stored in the XML DOM that I
    haven't yet imported.

    Please correct me if I'm wrong, or explain to me how I can use your
    code above?

    I experimented with "this," namely I set my button to,

    "<input name="myButton" type="button" onclick='window.location=' +
    this.value ..." // please ignore bad syntax here

    What I tried to do was dynamically set the "window.location=" based on
    the button's value. So, this HTML was created during the paint
    function. In the "populate the screen" function, I changed the value
    of the HTML's "myButton" based on the XML DOM. However, this approach
    hasn't worked.

    Thomas, I'm reading up on XSLT server-side now to transform the XML
    document into an HTML fragment. I don't know what that is, I'll read
    about it now. Thanks.
    Kevin, Jun 4, 2010
    #4
  5. Kevin wrote:

    [attribution restored]

    > [Garrett Smith wrote:]
    >> Consider using links for the menu items that need to act like links.
    >>
    >> <a id="CHeader" href="/csection/">
    >> <ul class="menu" id="CMenu">
    >> <li><a href="http://example.net/">eggs</a></li>
    >> </ul>

    >
    > [...]
    > I don't see how I can set the "<a href="http://example.net/">"
    > statically. The "http://example.net" is stored in the XML DOM that I
    > haven't yet imported.


    First, get your terminology right. You are _not_ importing a/the XML
    Document Object Model (DOM); you are importing, at most, an XML _document_.

    Second, you can either use server-side XSLT so that you don't need to import
    anything client-side, or you can use client-side XSLT to transform the XML
    document resource from the server (depends on your target environments), so
    that in both cases you might not need client-side scripting at all.

    > Please correct me if I'm wrong, or explain to me how I can use your
    > code above?


    You can use my correction of that code verbatim (of course, you will still
    have to do the CSS part). It is structurally what must be the result of the
    XML transformation process.

    > I experimented with "this," namely I set my button to,
    >
    > "<input name="myButton" type="button" onclick='window.location=' +
    > this.value ..." // please ignore bad syntax here


    The value of this button is this button's caption. That is most likely not
    where you want to navigate to. In fact, it does not make sense to use a
    button for navigation where a hyperlink would have sufficed.

    > Thomas, I'm reading up on XSLT server-side now to transform the XML
    > document into an HTML fragment. I don't know what that is, I'll read
    > about it now. Thanks.


    You're welcome.

    But keep in mind that this is a threaded one-to-many communications medium.
    So please keep at least the names of all relevant attribution lines, avoid
    summary followups (unless it's only a thank you to all), and trim your
    quotes to the relevant minimum, especially usually do not quote any
    signatures.

    <http://jibbering.com/faq/#posting>


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
    Thomas 'PointedEars' Lahn, Jun 5, 2010
    #5
    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. Frederick Grim

    scoping issue

    Frederick Grim, Sep 12, 2003, in forum: C++
    Replies:
    4
    Views:
    392
    Gianni Mariani
    Sep 14, 2003
  2. James Stroud

    Scoping issue with import

    James Stroud, Feb 28, 2005, in forum: Python
    Replies:
    2
    Views:
    300
    Steve Holden
    Mar 1, 2005
  3. Replies:
    12
    Views:
    715
    Bo Persson
    Aug 15, 2007
  4. John Nagle
    Replies:
    22
    Views:
    615
    Gregory Ewing
    Oct 16, 2010
  5. Pawel Szymczykowski

    require_gem scoping issue?

    Pawel Szymczykowski, Nov 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    87
    Pawel Szymczykowski
    Nov 7, 2006
Loading...

Share This Page