body onload when page is not loaded

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tomaz
    New Member
    • Oct 2008
    • 29

    #1

    body onload when page is not loaded

    I have following situation:

    <body onLoad= "Init()">


    my external javascript function (included in the html page):

    Code:
    function Init() {
      if (CtFlgTouchScreen == true) {
        var script = document.createElement("script"); 
        script.type = "text/javascript"; 
        script.src = "../js/vkb/keyboard.js";
        document.getElementsByTagName("head")[0].appendChild(script); 
        VKBInput();
        VKBTextArea();
        }
    // ALERT
    }
    this function is called before the total page is loaded.

    solution 1:
    add for example an alert box where you see "// ALERT", the scripts are not activated in the page. (a bad solution ofcourse)

    solution 2:
    i can add following line on the BOTTOM of the page:
    <script language="javas cript">Init()</script>
    I don't like this way around.

    solution 3:

    add "window.onl oad" in the function.
    this don't work. I added it at the end of the function.

    tnx
    Last edited by acoder; Nov 3 '08, 11:42 AM. Reason: Added [code] tags
  • joedeene
    Contributor
    • Jul 2008
    • 579

    #2
    Originally posted by tomaz
    ...this function is called before the total page is loaded....
    Not necessarily, according to this page the onLoad is triggered when the page is fully loaded.

    The browser triggers onLoad when the document is finished loading. The contents of onLoad is one or more JavaScript commands. So, for example, this <BODY ...> tag tells the browser to bring up an alert box once the page is completely loaded:
    But I don't think this occurs to plug-ins, like adobe flash player, etc...

    joedeene

    Comment

    • tomaz
      New Member
      • Oct 2008
      • 29

      #3
      still it doesn't seem this way.
      maybe because i use includes to add external javascripts.

      with the script at the bottom of the page it works.
      with the onload function it doesn't work.


      when i don't add the script below the page, nor i add it in the onload event of the body tag, i can use following:

      window.onload = function Init() {
      ....
      }

      still, if I don't add an alert command at the end of the function, the script wouldn't run correctly.

      Comment

      • tomaz
        New Member
        • Oct 2008
        • 29

        #4
        If I run my page locally, everything works fine.
        if I run it on my webserver, the script is not runned

        I don't understand it at all...

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          i guess this will have something to do with those 2 VKBxxxx-functions ... what do they do? an alert just breaks the 'flow' of the js-code-execution, its kind of a modal message - and 'time critical' or not correctly synchronized code like ajax-calls or something like this will work ... since the async calls have more time to get ready. is anything loaded with the mentioned functions? ...

          kind regards

          Comment

          • rnd me
            Recognized Expert Contributor
            • Jun 2007
            • 427

            #6
            you can't refer to the code in an external script before it loads.

            move those two function calls gits mentioned to keyboard.js, and the problem should go away.

            -or add your onload in keyboard.js.

            -or add the external script tag synchronously by document.writin g the external script tag instead of dom adding it.

            Comment

            • tomaz
              New Member
              • Oct 2008
              • 29

              #7
              i didn't replied on this topic.
              but to inform people who search this forum and have similar problems,
              this is the solution:

              -or add the external script tag synchronously by document.writin g the external script tag instead of dom adding it.

              thanks again

              Comment

              • rnd me
                Recognized Expert Contributor
                • Jun 2007
                • 427

                #8
                i wish everyone would come back just to say thanks...

                Comment

                Working...