Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Why this does not work?

Why this does not work?

Scheduled Pinned Locked Moved Web Development
javascripttoolsquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rgoyal
    wrote on last edited by
    #1

    Hi, Can u plz tell me that why this script doesn't work if I uncomment the commented line. JavaScript Test <!-- function clock() { var today=new Date(); var hr=today.getHours(); var min=today.getMinutes(); var sec=today.getSeconds(); if(hr < 10) hr=" "+hr; if(min < 10) min="0"+min; if(sec < 10) sec="0"+sec; var el=document.getElementById(1); el.innerHTML=hr+" : "+min+" : "+sec; document.clock.digits.value = hr + " : " + min + " : " + sec ; //document.write(hr+" : "+min+" : "+sec); setTimeout("clock()",1000); } //-->

    K 1 Reply Last reply
    0
    • R rgoyal

      Hi, Can u plz tell me that why this script doesn't work if I uncomment the commented line. JavaScript Test <!-- function clock() { var today=new Date(); var hr=today.getHours(); var min=today.getMinutes(); var sec=today.getSeconds(); if(hr < 10) hr=" "+hr; if(min < 10) min="0"+min; if(sec < 10) sec="0"+sec; var el=document.getElementById(1); el.innerHTML=hr+" : "+min+" : "+sec; document.clock.digits.value = hr + " : " + min + " : " + sec ; //document.write(hr+" : "+min+" : "+sec); setTimeout("clock()",1000); } //-->

      K Offline
      K Offline
      Kastellanos Nikos
      wrote on last edited by
      #2

      Naturally document.write(...); is placed in blocks inside the <body>. As the browser loads the html document if it finds a <code>document.write(...); code it appends the dynamically generated content and continue loading the rest of the document. What happens here is that the write() function is called through the onload event. Browser does not know where to place the new content since the parsing has allready ended. So it just replace the current document with the new one. (you assume it sould append it to the end, right? ;P) In IE the next fire of timer can't find the clock() function and gives you a jscript error. In mozilla the timer is clear along with the document. Try removing the onload event and put a clock() before . Now the first call to clock() does what you wanted, but the first fire of timer replaces the document since the caller (timer event that is..) can not be located as a place in the loaded document. you might call clock() within a loop but this also fire a 'script taking to long' box. I also try document.write("time is:"+hr+" : "+min+" : "+sec+""+clock+""); but by the time document.write is called, everything else is lost. - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234

      R 1 Reply Last reply
      0
      • K Kastellanos Nikos

        Naturally document.write(...); is placed in blocks inside the <body>. As the browser loads the html document if it finds a <code>document.write(...); code it appends the dynamically generated content and continue loading the rest of the document. What happens here is that the write() function is called through the onload event. Browser does not know where to place the new content since the parsing has allready ended. So it just replace the current document with the new one. (you assume it sould append it to the end, right? ;P) In IE the next fire of timer can't find the clock() function and gives you a jscript error. In mozilla the timer is clear along with the document. Try removing the onload event and put a clock() before . Now the first call to clock() does what you wanted, but the first fire of timer replaces the document since the caller (timer event that is..) can not be located as a place in the loaded document. you might call clock() within a loop but this also fire a 'script taking to long' box. I also try document.write("time is:"+hr+" : "+min+" : "+sec+""+clock+""); but by the time document.write is called, everything else is lost. - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234

        R Offline
        R Offline
        rgoyal
        wrote on last edited by
        #3

        You said: What happens here is that the write() function is called through the onload event. Browser does not know where to place the new content since the parsing has allready ended. Then why it does not happen with div and form element. As I am new to javascript I require more clear explanation. Thanks Ranjan

        K 1 Reply Last reply
        0
        • R rgoyal

          You said: What happens here is that the write() function is called through the onload event. Browser does not know where to place the new content since the parsing has allready ended. Then why it does not happen with div and form element. As I am new to javascript I require more clear explanation. Thanks Ranjan

          K Offline
          K Offline
          Kastellanos Nikos
          wrote on last edited by
          #4

          The write() function was intend to create dynamic content during the document load. line1 document.write('hello world!') line 3 This is what you see:

          line1
          hello world!
          line3

          As you can see the code is executed and the result is placed right when it is encounter. If the write() function is called due to an event then where should it place it's result? - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234

          R 1 Reply Last reply
          0
          • K Kastellanos Nikos

            The write() function was intend to create dynamic content during the document load. line1 document.write('hello world!') line 3 This is what you see:

            line1
            hello world!
            line3

            As you can see the code is executed and the result is placed right when it is encounter. If the write() function is called due to an event then where should it place it's result? - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234

            R Offline
            R Offline
            rgoyal
            wrote on last edited by
            #5

            Thanks a lot for your very clear response. Ranjan

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups