Any alternative to onload?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nntp

    #1

    Any alternative to onload?

    Is there anyway to do something exactly like onload, but without the word
    onload?

    I am trying to write inline js without onload, so I don't know how to
    trigger/start the script.


  • nntp

    #2
    Re: Any alternative to onload?

    > Is there anyway to do something exactly like onload, but without the word[color=blue]
    > onload?
    >
    > I am trying to write inline js without onload, so I don't know how to
    > trigger/start the script.
    >[/color]

    By the way, onmouseover, onblur are all ok. But they require the mouse to
    move to the object. Is there a way to make the script run 100%? BODY tag is
    not allowed either, so I have to either use a huge table for onmouseover. Is
    there any event like onmousemove?


    Comment

    • Fred Oz

      #3
      Re: Any alternative to onload?

      nntp wrote:[color=blue][color=green]
      >>Is there anyway to do something exactly like onload, but without the word
      >>onload?[/color][/color]

      You can include the script in the head of your document and kick it off
      without a "function" statement - a trivial example below:

      <html>
      <head><title> Auto-alert </title>
      <script type="text/javascript">
      alert('Hi from the head')
      </script>
      </head>
      <body onload="alert(' Hi from onload');">
      Here is the body
      </body>
      </html>

      Of course, instead of popping up an alert, you can call a function or
      whatever. Just remember that the script will execute when the browser
      reaches it and *will not* wait for the rest of the page to load.

      In the example, you should have a blank page until you dismiss the
      first alert, then the rest of the page is loaded, then the 'onload'
      alert is triggered. So if you are going to manipulate elements, put
      the script right at the bottom. Note also that images will likely not
      be loaded when your script loads and the user may have already clicked
      something before it executes, so it doesn't because the browser has
      already headed off to a new location...

      Still want to do it? Onload was created for a purpose.

      Cheers, Rob.

      Comment

      • Fred Oz

        #4
        Re: Any alternative to onload?

        nntp wrote:[color=blue][color=green]
        >>Is there anyway to do something exactly like onload, but without the word
        >>onload?
        >>
        >>I am trying to write inline js without onload, so I don't know how to
        >>trigger/start the script.
        >>[/color]
        >
        >
        > By the way, onmouseover, onblur are all ok. But they require the mouse to
        > move to the object. Is there a way to make the script run 100%? BODY tag is
        > not allowed either, so I have to either use a huge table for onmouseover. Is
        > there any event like onmousemove?
        >
        >[/color]

        I think it best you explain what you are trying to achieve. How can
        you have a table if you don't have a body? You can execute scripts by
        just putting commands inside <script> tags, a trivial example is below.

        Note that the script executes when the browser reaches it, not when it
        has actually rendered the page. Onload was created to run *after* the
        page is fully loaded. In the example, the alert in the header runs
        before the <body> tag is reached (or the </head> tag for that matter).

        The alert in the body will likely run before the page is rendered too,
        certainly before images have finished loading, and finally, after the
        page is loaded and rendered, the onload will run.

        Since you don't want to have a body tag, I presume you are writing the
        entire page using JavaScript, which seems quite pointless to me, but if
        that's what you want to do have a search for the thread on "writing
        slabs of HTML", it should be useful. There is a second example below.

        Cheers, Fred.


        *Example I:*

        <html><head><ti tle> Auto Run </title>

        <script type="text/javascript">
        doStuff();

        function doStuff() {
        alert('doStuff\ (\) says hello');
        }
        </script>
        </head>
        <body onload="alert(' Hi from onload');"
        style="backgrou nd-color: #eeeeff;">
        <h2>Here is the body</h2>
        <script type="text/javascript">
        alert('Here I am in the body');
        </script>
        </body>
        </html>



        *Example II:*

        <html><head><ti tle> Auto Run </title>
        </head>
        <script type="text/javascript">

        var a = [
        '<body><h2>her e is some text<\/h2>',
        '<form action="">',
        '<input type="button" value="Click me" ',
        'onclick="alert (\'Hi guys\');">',
        '<\/form>',
        '<\/body>',
        ];
        document.write( a.join(""));
        document.close( );
        </script>
        </html>

        Comment

        • Fred Oz

          #5
          Re: Any alternative to onload?

          nntp wrote:[color=blue][color=green]
          >>Is there anyway to do something exactly like onload, but without the word
          >>onload?
          >>
          >>I am trying to write inline js without onload, so I don't know how to
          >>trigger/start the script.
          >>[/color]
          >
          >
          > By the way, onmouseover, onblur are all ok. But they require the mouse to
          > move to the object. Is there a way to make the script run 100%? BODY tag is
          > not allowed either, so I have to either use a huge table for onmouseover. Is
          > there any event like onmousemove?
          >
          >[/color]

          I think it best you explain what you are trying to achieve. How can
          you have a table if you don't have a body? You can execute scripts by
          just putting commands inside <script> tags, a trivial example is below.

          Note that the script executes when the browser reaches it, not when it
          has actually rendered the page. Onload was created to run *after* the
          page is fully loaded. In the example, the alert in the header runs
          before the <body> tag is reached (or the </head> tag for that matter).

          The alert in the body will likely run before the page is rendered too,
          certainly before images have finished loading, and finally, after the
          page is loaded and rendered, the onload will run.

          Since you don't want to have a body tag, I presume you are writing the
          entire page using JavaScript, which seems quite pointless to me, but if
          that's what you want to do have a search for the thread on "writing
          slabs of HTML", it should be useful. There is a second example below.

          Cheers, Fred.


          *Example I:*

          <html><head><ti tle> Auto Run </title>

          <script type="text/javascript">
          doStuff();

          function doStuff() {
          alert('doStuff\ (\) says hello');
          }
          </script>
          </head>
          <body onload="alert(' Hi from onload');"
          style="backgrou nd-color: #eeeeff;">
          <h2>Here is the body</h2>
          <script type="text/javascript">
          alert('Here I am in the body');
          </script>
          </body>
          </html>



          *Example II:*

          <html><head><ti tle> Auto Run </title>
          </head>
          <script type="text/javascript">

          var a = [
          '<body><h2>her e is some text<\/h2>',
          '<form action="">',
          '<input type="button" value="Click me" ',
          'onclick="alert (\'Hi guys\');">',
          '<\/form>',
          '<\/body>',
          ];
          document.write( a.join(""));
          document.close( );
          </script>
          </html>

          Comment

          • Andrew Urquhart

            #6
            Re: Any alternative to onload?

            *nntp* wrote:[color=blue]
            > Is there anyway to do something exactly like onload, but without the word
            > onload?
            >
            > I am trying to write inline js without onload, so I don't know how to
            > trigger/start the script.[/color]

            Sorry, the problem you describe is not very clear. I suspect you mean
            that you can't add the onload attribute to your body tag (for various
            reasons), but nevertheless require an onload ability? In which case the
            script: window.onload = MyFunction; would probably do the job for you.

            Then again you mention 'write inline script' and this could imply the
            use of document.write( ) - if so you likely won't want to be executing
            that after the document has been loaded.

            Anyhow, not knowing what you really want, here's a snippet of script
            that attempts to execute function MyFunction() after the document has
            loaded and tries not to use "onload" unless it's the only available
            method of invoking functions after the document has loaded:


            if (window.attachE vent) {
            window.attachEv ent('onload', MyFunction);
            }
            else if (window.addEven tListener) {
            window.addEvent Listener("load" , MyFunction, false);
            }
            else {
            window.onload = MyFunction;
            }

            --
            Andrew Urquhart
            - FAQ: http://www.jibbering.com/faq/
            - Archive: http://groups.google.com/groups?grou...ang.javascript
            - Reply: http://andrewu.co.uk/contact/

            Comment

            • nntp

              #7
              Re: Any alternative to onload?

              <body> tag is not allowed
              <table> is ok

              onload is not allowed
              onmouseover is ok.

              I got it myself
              I used <div> + onmouseover
              it is almost equal to body+onload, except when the mouse is not on the
              screen.

              Thanks.


              Comment

              • Lee

                #8
                Re: Any alternative to onload?

                nntp said:[color=blue]
                >
                ><body> tag is not allowed
                ><table> is ok
                >
                >onload is not allowed
                >onmouseover is ok.[/color]

                When you're asking for a way to work around some limitation
                (eg, no body tag), it's a really good idea to explain why you
                face that limitation. If you don't, people are going to waste
                time asking, anyway, and you probably won't get as much help.

                I've had some experiences in which the "page" I was writing
                was actually going to be incorporated into a standard <body>
                by server-side code, so that's what I'm picturing here.

                Comment

                • Eric Bohlman

                  #9
                  Re: Any alternative to onload?

                  Lee <REM0VElbspamtr ap@cox.net> wrote in news:cl0fc4017l n@drn.newsguy.c om:
                  [color=blue]
                  > When you're asking for a way to work around some limitation
                  > (eg, no body tag), it's a really good idea to explain why you
                  > face that limitation. If you don't, people are going to waste
                  > time asking, anyway, and you probably won't get as much help.[/color]

                  And some people will jump to the conclusion that you're a student asking
                  other people to do his homework for him.

                  Comment

                  • Randy Webb

                    #10
                    Re: Any alternative to onload?

                    nntp wrote:
                    [color=blue]
                    > <body> tag is not allowed
                    > <table> is ok
                    >
                    > onload is not allowed
                    > onmouseover is ok.
                    >
                    > I got it myself
                    > I used <div> + onmouseover
                    > it is almost equal to body+onload, except when the mouse is not on the
                    > screen.[/color]

                    Thats not even close to true. The body/windows onload will only fire
                    once until the page is reloaded. onmouseover will fire repeatedly if the
                    mouse is moved in/out of the div. That makes a *huge* difference.

                    But the whole question almost sounds like homework.

                    --
                    Randy
                    comp.lang.javas cript FAQ - http://jibbering.com/faq

                    Comment

                    Working...