Passing a value to event handler

Discussion in 'Javascript' started by Steve, Nov 4, 2007.

  1. Steve

    Steve Guest

    The following javascript file works:

    window.onload = writeMessage;
    function writeMessage() {
    document.getElementById("helloMessage").innerHTML = "bottom";
    }

    But when I pass a value to the event handler like this:

    window.onload = writeMessage("bottom");
    function writeMessage(text) {
    document.getElementById("helloMessage").innerHTML = text;
    }

    I get the error "document.getElementById("helloMessage") has no
    properties"

    How can I pass a value to the event handler?
     
    Steve, Nov 4, 2007
    #1
    1. Advertising

  2. Steve

    VK Guest

    On Nov 4, 9:37 pm, Steve <> wrote:

    > But when I pass a value to the event handler like this:
    >
    > window.onload = writeMessage("bottom");
    > function writeMessage(text) {
    > document.getElementById("helloMessage").innerHTML = text;
    >
    > }
    >
    > I get the error "document.getElementById("helloMessage") has no
    > properties"
    >
    > How can I pass a value to the event handler?


    window.onload = writeMessage("bottom"); means:
    "- Execute writeMessage() function with argument "bottom" and assign
    the return value as a function reference to window.onload handler."

    To do you what you really want you may go two ways: 1) by the ancient
    one and 2) over augmentation.

    1)
    <html>
    ....
    <body onload="writeMessage('bottom')">
    ....

    2)
    ....
    function writeMessage() {
    text = arguments.callee.args['text'] || "No hello message";
    document.getElementById("helloMessage").innerHTML = text;
    }

    writeMessage.args = {"text":"hello", "foo":"bar"};
    window.onload = writeMessage;
     
    VK, Nov 4, 2007
    #2
    1. Advertising

  3. Steve

    Steve Guest

    Many thanks VK. I have decided to use the ancient method :)

    What are the pro's and con's of having the onload in the html file
    versus having it in the javascript?

    Cheers,

    On Nov 4, 8:23 pm, VK <> wrote:
    > On Nov 4, 9:37 pm, Steve <> wrote:
    >
    > > But when I pass a value to the event handler like this:

    >
    > > window.onload = writeMessage("bottom");
    > > function writeMessage(text) {
    > > document.getElementById("helloMessage").innerHTML = text;

    >
    > > }

    >
    > > I get the error "document.getElementById("helloMessage") has no
    > > properties"

    >
    > > How can I pass a value to the event handler?

    >
    > window.onload = writeMessage("bottom"); means:
    > "- Execute writeMessage() function with argument "bottom" and assign
    > the return value as a function reference to window.onload handler."
    >
    > To do you what you really want you may go two ways: 1) by the ancient
    > one and 2) over augmentation.
    >
    > 1)
    > <html>
    > ...
    > <body onload="writeMessage('bottom')">
    > ...
    >
    > 2)
    > ...
    > function writeMessage() {
    > text = arguments.callee.args['text'] || "No hello message";
    > document.getElementById("helloMessage").innerHTML = text;
    >
    > }
    >
    > writeMessage.args = {"text":"hello", "foo":"bar"};
    > window.onload = writeMessage;
     
    Steve, Nov 4, 2007
    #3
  4. Steve

    VK Guest

    On Nov 4, 11:09 pm, Steve <> wrote:
    > What are the pro's and con's of having the onload in the html file
    > versus having it in the javascript?


    By using the original way over <body> arguments you are decreasing
    your code flexibility, because your JavaScript and HTML are getting
    tied up. It means that in order to change the script (say to call
    another function onload or to call it with another argument) each time
    you have to change your HTML as well. If this is a solution for
    yourself and if it doesn't matter to you - then it doesn't matter at
    all :)

    Also in the context of the modern "content / presentation /
    scripting / everything / separation" such code may look irritating for
    some evangelistic eyes - so leading to narrations about "separation
    rules of the modern Web" etc. If it doesn't matter to you - then it
    doesn't matter at all, just keep some firm but polite "go to hell"
    answers for such cases :)
     
    VK, Nov 4, 2007
    #4
  5. Steve

    RobG Guest

    On Nov 5, 5:23 am, VK <> wrote:
    > On Nov 4, 9:37 pm, Steve <> wrote:
    >
    > > But when I pass a value to the event handler like this:

    >
    > > window.onload = writeMessage("bottom");
    > > function writeMessage(text) {
    > > document.getElementById("helloMessage").innerHTML = text;

    >
    > > }

    >
    > > I get the error "document.getElementById("helloMessage") has no
    > > properties"

    >
    > > How can I pass a value to the event handler?

    >
    > window.onload = writeMessage("bottom"); means:
    > "- Execute writeMessage() function with argument "bottom" and assign
    > the return value as a function reference to window.onload handler."
    >
    > To do you what you really want you may go two ways: 1) by the ancient
    > one and 2) over augmentation.
    >
    > 1)
    > <html>
    > ...
    > <body onload="writeMessage('bottom')">
    > ...
    >
    > 2)
    > ...
    > function writeMessage() {
    > text = arguments.callee.args['text'] || "No hello message";
    > document.getElementById("helloMessage").innerHTML = text;
    >
    > }
    >
    > writeMessage.args = {"text":"hello", "foo":"bar"};
    > window.onload = writeMessage;


    Method 2 seems somewhat convoluted. The list is a bit short:

    3)

    window.onload = function(){
    writeMessage(arg1, arg2, ...);
    }

    4) Various method using attachEvent/addEventListener:

    <URL:
    http://groups.google.com.au/group/c...attachEvent addeventlistener#93561d5a2ebdb4aa
    >



    --
    Rob
     
    RobG, Nov 4, 2007
    #5
  6. Steve

    VK Guest

    On Nov 4, 11:50 pm, RobG <> wrote:
    > Method 2 seems somewhat convoluted. The list is a bit short:
    >
    > 3)
    >
    > window.onload = function(){
    > writeMessage(arg1, arg2, ...);
    >
    > }


    Yeah, that would be much simpler for the situation in question. To
    keep the numbering fair :) - that one above should be the number 2,
    not 3, because it is internally the equivalent of 1) <body
    onload="writeMessage(arg1,...)">
     
    VK, Nov 6, 2007
    #6
  7. Steve a écrit :
    > The following javascript file works:
    >
    > window.onload = writeMessage;
    > function writeMessage() {
    > document.getElementById("helloMessage").innerHTML = "bottom";
    > }
    >
    > But when I pass a value to the event handler like this:
    >
    > window.onload = writeMessage("bottom");
    > function writeMessage(text) {
    > document.getElementById("helloMessage").innerHTML = text;
    > }
    >
    > I get the error "document.getElementById("helloMessage") has no
    > properties"
    >
    > How can I pass a value to the event handler?
    >


    window.onload = function() {
    writeMessage("bottom");
    };
     
    Bruno Desthuilliers, Nov 8, 2007
    #7
  8. Steve wrote:
    > Many thanks VK. I have decided to use the ancient method :)


    The approach works since the first days of DOM scripting, but it hardly
    qualifies as "ancient".

    > What are the pro's and con's of having the onload in the html file
    > versus having it in the javascript?


    The former is standardized (since HTML 3.2) and (therefore) interoperable,
    the latter is not.

    I'd strongly advise you not to listen to VK's fairytales, and to read
    previous threads about DOM scripting instead.

    Please don't top-post, reply below trimmed quotes (as explained and
    recommended in the FAQ http://jibbering.com/faq/).


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, Nov 8, 2007
    #8
  9. Steve

    Darko Guest

    On Nov 8, 7:56 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > Steve wrote:
    > > Many thanks VK. I have decided to use the ancient method :)

    >
    > The approach works since the first days of DOM scripting, but it hardly
    > qualifies as "ancient".
    >
    > > What are the pro's and con's of having the onload in the html file
    > > versus having it in the javascript?

    >
    > The former is standardized (since HTML 3.2) and (therefore) interoperable,
    > the latter is not.
    >


    What's non-standard with:

    window.onload = function()
    {
    // ...
    };

    ?
     
    Darko, Nov 9, 2007
    #9
  10. Steve

    RobG Guest

    On Nov 9, 11:50 am, Darko <> wrote:
    > On Nov 8, 7:56 pm, Thomas 'PointedEars' Lahn <>
    > wrote:
    > > Steve wrote:

    [...]
    > > > What are the pro's and con's of having the onload in the html file
    > > > versus having it in the javascript?

    >
    > > The former is standardized (since HTML 3.2) and (therefore) interoperable,
    > > the latter is not.

    >
    > What's non-standard with:
    >
    > window.onload = function()


    The onload attribute of the body element has been included in the W3C
    HTML recommendation since version 3.2 - hence Thomas' assertion that
    it is *standarised*.

    The onload property of the window object is part of DOM 0, which is
    not a standard, but could be said to be just as standardised as the
    HTML body element's onload attribute referred to above.

    I think it is a matter of opinion as to whether one is more
    standardised than the other. If real standards are considred, the
    discussion would have to turn to ISO standard HTML[1], where the DTD
    doesn't include script elements or attributes for intrinisic events
    (i.e. there is no onload attribute, and no way of specifying a code
    that might do something with it), so consideration of actual standards
    renders the entire conversation moot.

    1. <URL: https://www.cs.tcd.ie/15445/15445.html >


    --
    Rob
     
    RobG, Nov 9, 2007
    #10
  11. RobG wrote:
    > On Nov 9, 11:50 am, Darko <> wrote:
    >> On Nov 8, 7:56 pm, Thomas 'PointedEars' Lahn <>
    >> wrote:
    >>> Steve wrote:

    > [...]
    >>>> What are the pro's and con's of having the onload in the html file
    >>>> versus having it in the javascript?
    >>> The former is standardized (since HTML 3.2) and (therefore)
    >>> interoperable, the latter is not.

    >> What's non-standard with:
    >>
    >> window.onload = function()

    >
    > The onload attribute of the body element has been included in the W3C
    > HTML recommendation since version 3.2 - hence Thomas' assertion that it
    > is *standarised*.


    Exactly.

    > The onload property of the window object is part of DOM 0, which is not a
    > standard, but could be said to be just as standardised as the HTML body
    > element's onload attribute referred to above.


    Wishful thinking.

    > I think it is a matter of opinion as to whether one is more standardised
    > than the other.


    It isn't. One is specified in actually several Web standard documents, the
    other one is not.

    > If real standards are considred,


    ISO standards are no more "real standards" than W3C standards.

    > the discussion would have to turn to ISO standard HTML[1], where the DTD
    > doesn't include script elements or attributes for intrinisic events (i.e.
    > there is no onload attribute, and no way of specifying a code that might
    > do something with it),


    Which is one aspect of why it is mostly useless nowadays.

    > so consideration of actual standards renders the entire conversation
    > moot.


    Non sequitur.

    > 1. <URL: https://www.cs.tcd.ie/15445/15445.html >


    Whether you like it or not, the W3C Recommendations, especially the HTML 3.2
    and 4.01 Specifications, *are* industrial Web standards.

    http://www.rfc-editor.org/rfc/rfc2854.txt
    http://www.w3.org/Consortium/


    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, Nov 9, 2007
    #11
  12. Randy Webb wrote:
    > Darko said the following on 11/8/2007 8:50 PM:
    >> What's non-standard with:
    >>
    >> window.onload = function()
    >> {
    >> // ...
    >> };

    >
    > Please don't get him started. He is going to tell you that the "window"
    > object is proprietary and then it just goes on from there.


    It *is* still a proprietary, host-defined property of the Global Object,
    hence all of its properties are proprietary and host-defined.

    There are efforts underway to standardize the interface, however that draft
    still lacks commonly used features: http://www.w3.org/TR/Window/ Until that
    is resolved and implemented, there is no standard for that interface. Period.

    And if you would have applied common sense, you would have recognized the
    reported inability of your own mobile device's user agent to execute
    window.alert() as a sure sign that the object and its implementations are
    not a standard at all.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Nov 9, 2007
    #12
    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. DC Gringo
    Replies:
    5
    Views:
    524
    =?Utf-8?B?U3VyZXNo?=
    Mar 3, 2004
  2. Kyzer
    Replies:
    2
    Views:
    783
    Duane Hebert
    May 6, 2005
  3. Replies:
    1
    Views:
    734
    Damien
    Feb 22, 2007
  4. DC Gringo

    passing a value to an event handler from dropdownlist

    DC Gringo, Mar 2, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    189
  5. Adi
    Replies:
    2
    Views:
    199
Loading...

Share This Page