Embarrassing question: How to un-inline this simple JavaScript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShameOnMe
    New Member
    • Jun 2010
    • 4

    Embarrassing question: How to un-inline this simple JavaScript?

    Code from MDC:

    Code:
    <html>
    <head>
    <title>event object parameter example</title>
    <script type="text/javascript">
    function showCoords(evt){
      alert(
        "clientX value: " + evt.clientX + "\n" +
        "clientY value: " + evt.clientY + "\n"
      );
    }
    </script>
    </head>
    <body onmousedown="showCoords(event)">
    <p>To display the mouse coordinates click anywhere on the page.</p>
    </body>
    </html>
    I've tried to bruteforce a correct piece of code, but to no avail. Eg:

    <body onmousedown="sh owCoords(event) ">
    =>
    document.body.o nmousedown=show Coords(event);
    Last edited by Dormilich; Jun 9 '10, 03:12 PM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the event call is wrong. any event handler defined in JavaScript expects a function or function reference to be passed.

    Code:
    document.body.onmousedown = showCoords;

    Comment

    • ShameOnMe
      New Member
      • Jun 2010
      • 4

      #3
      Thanks for the response, but that option was part of my bruteforcing session i.e. following code won't alert any mouse coordinates in Firefox:

      Code:
      <html>
      <head>
      <title>event object parameter example</title>
      <script type="text/javascript">
      function showCoords(evt){
       alert(
        "clientX value: " + evt.clientX + "\n" +
        "clientY value: " + evt.clientY + "\n"
       );
      }
      document.body.onmousedown = showCoords;
      </script>
      </head>
      <body>
      <p>To display the mouse coordinates click anywhere on the page.</p>
      </body>
      </html>

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        simple error of timing, you can’t apply an event handler to a non-existing element (at the time you try to invoke document.body.o nmousedown, <body> doesn’t exist yet)

        Comment

        • ShameOnMe
          New Member
          • Jun 2010
          • 4

          #5
          Thanks for the response, but that option was _also_ part of my bruteforcing session i.e. following code won't alert any mouse coordinates in Firefox:

          Code:
          <html>
          <head>
          <title>event object parameter example</title>
          <script type="text/javascript">
          onload=function(){
           function showCoords(evt){
            alert(
             "clientX value: " + evt.clientX + "\n" +
             "clientY value: " + evt.clientY + "\n"
            );
           }
           document.body.onmousedown=showCoords;
          };
          </script>
          </head>
          <body>
          <p>To display the mouse coordinates click anywhere on the page.</p>
          </body>
          </html>

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            then you should turn on JavaScript, because the posted code worked as expected for me.

            Comment

            • ShameOnMe
              New Member
              • Jun 2010
              • 4

              #7
              Unfortunately it is on. This time I tested the following code:

              Code:
              <html>
              <head>
              <title>event object parameter example</title>
              <script type="text/javascript">
              onload=function(){
               function showCoords(evt){
                evt=evt||event;
                alert(
                 "clientX value: " + evt.clientX + "\n" +
                 "clientY value: " + evt.clientY + "\n"
                );
               }
               document.body.onmousedown=showCoords;
              };
              </script>
              </head>
              <body>
              <p>To display the mouse coordinates click anywhere on the page.</p>
              </body>
              </html>
              ...and got the following results:

              IE: Working
              Chrome: Working
              Safari: Working

              Opera: Not working
              Firefox: Not working

              Could this be about non-working browsers not supporting onmousedown event on certain elements (e.g. body)? I guess not because it's working inline.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                I tested your code in Firefox and Opera and it was working.

                Comment

                Working...