Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Dec 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question error: null or not an object in javascript

    Hello

    I am using a timer which goes down from 2 minutes to zero when 'jsp' is called. Timer is written in javascript. I am getting an error that document.form1.timerMin.value=<%=min%> is null or not an object.
    Here is the code (t.jsp):
    <%
    /* Default timer */
    int min = 2;
    int sec = 5;

    /* Check for timer params */
    try {
    String timerMin = request.getParameter("timerMin");
    if (timerMin==null)timerMin="";
    if (timerMin != null) {
    min = Integer.parseInt(timerMin);
    }
    String timerSec = request.getParameter("timerSec");
    if (timerSec==null)timerSec="";
    if (timerSec != null) {
    sec = Integer.parseInt(timerSec);
    }
    }

    catch(Exception m) {
    System.out.println("generated exception..............:"+m);
    }

    %>

    <script language="JavaScript" type="text/javascript">
    <!--

    /* Initialise timer */
    var min = <%=min%>; // set the minutes
    var sec = <%=sec%>; // set the seconds
    function countDown() {
    sec--;
    if (sec == -1) {
    sec = 59;
    min--;
    }

    /* Write timer state to hidden form fields */
    document.form1.timerMin.value=<%=min%>
    document.form1.timerSec.value=<%=sec%>;

    time = (min<=9 ? "0" + min : min) + " min and " + (sec<=9 ? "0" + sec : sec) + " sec ";
    document.getElementById('theTime').innerHTML = time;

    SD=window.setTimeout("countDown()", 1000);
    if (min == 0 && sec == 0) {
    window.clearTimeout(SD);
    }
    }

    window.onload = countDown();
    -->
    </script>

    <style type="text/css">
    <!--
    .timeClass {
    font-family:arial,verdana,helvetica,sans-serif;
    font-weight:normal;
    font-size:10pt;
    }
    -->
    </style>

    <table width="100%">
    <tr><td width="100%" align="right">
    <span id="theTime" class="timeClass"></span>
    </td></tr>
    <form method="post" name="form1" id="form1" action="t.jsp"
    <td width="50%" valign="middle">
    <input type="hidden" name="timerMin" value="<%=min%>">
    <input type="hidden" name="timerSec" value="<%=sec%>">
    <div align="center"><a href="javascript:form1.submit()" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image3','','/lms/images/submit_0.gif',1)"><img name="Image3" border="0" src="/lms/images/submit_0.gif" width="70" height="30"></a></div>
    </td>

    </form>

    Looking forward for an early reply.

    Thanks

  • #2
    VR2
    VR2 is offline
    New Coder
    Join Date
    Jul 2006
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think its because

    Code:
    document.form1.timerMin.value=<%=min%>
    is executed as the page is downloaded, not after (or ON) the page load, where it SHOULD be.

    Y'see, at the time you execute this - there is NO DOCUMENT. Thus "document" is undefined and trying to access any of its properties or methods will throw an exception.

    Actually, my bad - the indentation threw me (lack of) - the countDown() function goes on longer doesn't it!

    Ok so

    Code:
    document.form1.timerMin.value=<%=min%>
    Is missing a semi colon. Also it should have quotes around the value, since its effectively string (as a form field value).

    Other than that - I'm not understanding the code, since <%=min%> is always a static "hardcoded" number, why are you using it in your countDown loop? Should it be..

    Code:
    document.form1.timerMin.value= min;
    as in the client side Javascript variable called "min"?

    I think maybe using Java on the server and javascript on the client is kinda confusing to deal with!!!
    Last edited by VR2; 07-11-2006 at 02:27 PM.

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    if your going to be printing numbers from your JSP page you don't need quotes around <%=min%> but if you're using strings, you need "<%=min%>"

    The semicolon is not a requirement at the end of statements in javascript.

    You are missing the closing angle bracket on your form tag, this is invalid markup and could definitely cause you problems.

    Also, if your script attempts to access an element before it is loaded, as the previous poster suggested, you will have an error, and it's best to begin your script onload.

  • #4
    New Coder
    Join Date
    Dec 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    I did like just putting min instead of <%=min%> or "<%=min%>", even then it didn't worked. I gave whole code, if U copy, paste it & run the script, actual error will be notice.
    Looking forward for an early reply.

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    got it, it's this line:

    window.onload = countDown();

    Two problems:
    1) it should be document.onload
    2) it should be countDown; without the parens

    with the parens, you're actually calling the function and assigning its return value to window.onload, which is not what you want, you want the function assigned, not the return of the function.

    So make the line:

    document.onload = countDown;

  • #6
    New Coder
    Join Date
    Dec 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Hi Beagle
    Thanks error got removed but timer is not shown on screen. Actually, countdown timer starts from 2 minutes (2.00) & ends at (0.0) . When jsp (t.jsp) is call, timer starts with 2:00 & goes upto 1:30 then I click submit & that jsp(t.jsp) is called again & timer starts from 1:30 & gets decremented & again I click submit & same jsp(t.jsp) is called again & so on. Though error is removed but countdown timer is not shown.
    When I gave -->alert(sec--); it didn't executed that means timer is not working.
    Looking forward for an early reply.

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    2 minutes in javascript is 120000 not 2 and 5 seconds in javascript is 5000 not 5.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    New Coder
    Join Date
    Dec 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Actually, my application is of online test. Student starts the test, timer is displayed, a Question with 3 options is displayed, selecting 1 option(radio button) he clicks submit & jsp(t.jsp) is called with different question & options with timer decremented. After selecting 1 option(radio button) he again clicks submit & that same jsp(t.jsp) is called with timer decremented & so on. So for e.g if I have set test for 2 minutes, 5 seconds, timer will decrement to zero. But after doing below changes as suggested by Beagle, I found timer is not functioning since alert message is also not show. So how to solve?
    Looking forward for an early reply.
    Thanks

  • #9
    New Coder
    Join Date
    Dec 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Hey, no one gave answer to my query.

  • #10
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Try putting the countDown function back on window.onload, I think I was wrong about that one.

  • #11
    New Coder
    Join Date
    Dec 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    I tried window.onload even then error message is display that's why I said just copy paste the code & run it.
    Looking forward for an early reply.
    Thanks

  • #12
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    I don't have a JSP setup anywhere, so I can't just copy your code.
    Also, you haven't posted your upated code, so I can't test it anyway.

    Please post all of your new code after the changes you've made so I can take a look at it.

  • #13
    New Coder
    Join Date
    Dec 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    No change in code except window.onload = countDown();

    If I use window.onload = countDown; (without parenthesis) then no error but no display of timer that is timer (countdown function) is not excuted. If I give alert in countdown function, it doesn't show any alert messg.

    If I use window.onload = countDown();(with parenthesis) then error messg of
    document.form1.timerMin.value= min; is null or not an object is display.

    So what's the way out?

    Looking forward for an early reply.

    Thanks

  • #14
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Delete this:
    Code:
    SD=window.setTimeout("countDown()", 1000);
    if (min == 0 && sec == 0) {
    window.clearTimeout(SD);
    }
    }
    
    window.onload = countDown();
    replace with:

    Code:
    var SD;
    window.onload = function()
    {
      SD = window.setTimeout("countDown()", 1000);
    }
    -->
    </script>
    Try that, I think the problem is that I wasn't understanding everything your code was doing (I'm kind of rushed at work sometimes). This might cause your countdown to go on forever, but try it anyway, and get back to me.

  • #15
    New Coder
    Join Date
    Dec 2004
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Hi Beagle

    I replace the code even then it didn't worked . Here is the complete code :

    <script language="JavaScript" type="text/javascript">
    <!--
    <%
    /* Default timer */
    int min = 2;
    int sec = 5;

    /* Check for timer params */
    try {
    String timerMin = request.getParameter("timerMin");
    if (timerMin==null)timerMin="";
    System.out.println("timerMin..."+timerMin);
    if (timerMin != null) {
    min = Integer.parseInt(timerMin);
    }
    String timerSec = request.getParameter("timerSec");
    if (timerSec==null)timerSec="";
    System.out.println("timerSec..."+timerSec);
    if (timerSec != null) {
    sec = Integer.parseInt(timerSec);
    }
    }

    catch(Exception m) {
    System.out.println("generated exception..............:"+m);

    }

    %>
    /* Initialise timer */
    var min = <%=min%>; // set the minutes
    alert (min);
    var sec = <%=sec%>; // set the seconds
    alert (sec);
    alert("hi");
    function countDown() {
    sec--;
    alert(sec--);
    if (sec == -1) {
    sec = 59;
    min--;
    }

    /* Write timer state to hidden form fields */
    alert(<%=min%>);

    document.form1.timerMin.value=min;
    document.form1.timerSec.value=sec;

    time = (min<=9 ? "0" + min : min) + " min and " + (sec<=9 ? "0" + sec : sec) + " sec ";
    document.getElementById('theTime').innerHTML = time;


    var SD;
    window.onload = function()
    {
    SD = window.setTimeout("countDown()", 1000);
    }
    }

    window.onload = countDown;
    -->
    </script>

    <style type="text/css">
    <!--
    .timeClass {
    font-family:arial,verdana,helvetica,sans-serif;
    font-weight:normal;
    font-size:10pt;
    }
    -->
    </style>
    <span id="theTime" class="timeClass"></span>
    <form method="post" name="form1" id="form1" action="t.jsp">
    <input type="hidden" name="timerMin" value="<%=min%>">
    <input type="hidden" name="timerSec" value="<%=sec%>">
    </form>


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •