This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes reloading issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "reloading issue" Watch "reloading issue" New topic
Author

reloading issue

Alex George
Ranch Hand

Joined: Mar 23, 2007
Posts: 51
I do have the following code in the header jsp. The idea is that I want to collect the visitor time using javascript and
set it into session so the cotroller part of the application can call an appropriate method to use this.
But the fact is that when I monitor the execution in the console window, I see it getting executed many times and
it wouldn't stop executing. It keep submitting the form again and again and wouldn't stop.
Anybody can think of some other way to make it work?


<head>
<SCRIPT LANGUAGE="JavaScript">
function fnSubmit() {
window.document.form1.submit();
return;
}
</SCRIPT>
</head>

<body onloa="return fnSubmit()">
<form name="form1">
<script language="JavaScript">
<!--
var visitortime = new Date();
document.write('<input type="hidden" name="VisitorTime" ');
if(visitortime) {
document.write('value="' + visitortime + '"> ');
}
else {
document.write('value="JavaScript not Date() enabled">');
}// -->
</script>
<noscript><input type="hidden"
name="VisitorTime" value="Browser not JavaScript enabled">
</noscript>

</form>
<%
String VisitorTime = request.getParameter("VisitorTime");


System.out.println("####################################");
System.out.println("### Visitor Time is " + VisitorTime + "###");
System.out.println("####################################");
%>

</body>


Thanks in advance.
[ December 10, 2007: Message edited by: Alex George ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Because you keep loading the same page. Onload is being fired everytime since that is what it does. You would need to make the page smart [don't output code] or have the page submit to a different location.

Eric
Alex George
Ranch Hand

Joined: Mar 23, 2007
Posts: 51
How can I make it smart. I output the code just to see if the value is getting set in the session.
I highly appreciate your help.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
  • Have your server output the code when you get the value back you do not display it again.
  • You add a cookie that determines if you need to post back [if cookies are disabled the user will be stuck in a loop.]
  • You post the page to another page where the code does not exist.
  • You just wait to the user posts back the page to see the value.

  • Eric
    Alex George
    Ranch Hand

    Joined: Mar 23, 2007
    Posts: 51
    header is getting inserted in all the pages. So I cannot really submit this anywhere.
    Eric Pascarello
    author
    Rancher

    Joined: Nov 08, 2001
    Posts: 15376
        
        6
    Than you need to say hey I go this already, do not display it.

    Eric
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18570
        
        8

    Originally posted by Alex George:
    The idea is that I want to collect the visitor time using javascript and
    set it into session so the cotroller part of the application can call an appropriate method to use this.
    Maybe I am missing something, but wouldn't it be easier to just collect the visitor time directly in that JSP, rather than generating HTML that sends a second request?
    Alex George
    Ranch Hand

    Joined: Mar 23, 2007
    Posts: 51
    What Mr.Eric Pascarello said worked for me.
    But how can I get the visitor time in the jsp without using javascript?
     
    GeeCON Prague 2014
     
    subject: reloading issue