File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Could I make a automatic call for ajax method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Could I make a automatic call for ajax method" Watch "Could I make a automatic call for ajax method" New topic
Author

Could I make a automatic call for ajax method

swati mittal
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Hi,

I have a website.
I want to show current date and time on the top of my site.
And I want to refresh that time in every second without refreshing whole page.
How it will be possible to refresh that particular portion without clicking anywhere in site.

Thanks in advance....
Mansi Mishra
Ranch Hand

Joined: Dec 26, 2008
Posts: 50
@Swati

Please check this...

http://www.neowin.net/forum/lofiversion/index.php/t447063.html


http://splashpress.blogspot.com
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Why do you want to use AJAX for that? Just client-side JavaScript should be sufficient for that, no?


Ping & DNS - updated with new look and Ping home screen widget
swati mittal
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
@ Ulf Dittmer

Please tell me how i can use javascript to refresh particular part of web page.
Mansi Mishra
Ranch Hand

Joined: Dec 26, 2008
Posts: 50
Add the page name at the top of the javascript
var page = "test.jsp";
<script type="text/javascript">
var page = "test.jsp";
function ajax(url,target)
{
// native XMLHttpRequest object
document.getElementById(target).innerHTML = 'sending...';
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = function() {ajaxDone(target);};
req.open("GET", url, true);
req.send(null);
// IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = function() {ajaxDone(target);};
req.open("GET", url, true);
req.send();
}
}
setTimeout("ajax(page,'scriptoutput')", 10000);
}

function ajaxDone(target) {
// only if req is "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200 || req.status == 304) {
results = req.responseText;
document.getElementById(target).innerHTML = results;
} else {
document.getElementById(target).innerHTML="ajax error:\n" +
req.statusText;
}
}
}
</script>


Adjust the reload time here:
setTimeout("ajax(page,'scriptoutput')", 10000);

Call the script onload of the form
<body onload="ajax(page,'scriptoutput')">

Define an area for the content:

Current Server date & time (updated every 10 seconds):

<span id="scriptoutput"></span>



This works for me

Mansi
lin qun
Greenhorn

Joined: Feb 20, 2009
Posts: 5
<script language="javascript">
var cuurTime=null;
function setCurrTime(){
cuurTime=new Date();
var divTime=document.createElement("id");
divTime.setAttribute("id","timeDiv");
document.body.appendChild(divTime);
divTime.innerHTML=cuurTime.toString();
}
window.setInterval(function(){
var divTime=document.getElementById("timeDiv");
var cuurTime=new Date();
divTime.innerHTML=cuurTime.toString();
},1000);
</script>

this works for me!
hopes for you
swati mittal
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
@ lin qun

How i will call this javascript function.
Because I want to display date and time without clicking any where.
lin qun
Greenhorn

Joined: Feb 20, 2009
Posts: 5
<html>
<head>
<title>MyHtml.html</title>
<script language="javascript">
var cuurTime=null;
function setCurrTime(){
cuurTime=new Date();
var divTime=document.createElement("id");
divTime.setAttribute("id","timeDiv");
document.body.appendChild(divTime);
divTime.innerHTML=cuurTime.toString();
}
window.setInterval(function(){
var divTime=document.getElementById("timeDiv");
var cuurTime=new Date();
divTime.innerHTML=cuurTime.toString();
},1000);
</script>
</head>

<body onload="setCurrTime();">
</body>
</html>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
swati mittal wrote:How i will call this javascript function.

You don't. Just add it to your web page and watch what happens.
swati mittal
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
thanks to both of you.

its working fine.
But My application is already using some other function at body onload so how i can use this one.
because there will be only one "onload"
lin qun
Greenhorn

Joined: Feb 20, 2009
Posts: 5
swati mittal wrote:thanks to both of you.

its working fine.
But My application is already using some other function at body onload so how i can use this one.
because there will be only one "onload"


put all the function that you want to onload first into a new function bind to the onload event
Mansi Mishra
Ranch Hand

Joined: Dec 26, 2008
Posts: 50
swati mittal wrote:thanks to both of you.

its working fine.
But My application is already using some other function at body onload so how i can use this one.
because there will be only one "onload"


you can have it semicolon separated....like this

<body onload="setFocusOnLoad();ajax(page,'scriptoutput');">

Mansi
swati mittal
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Mansi. Mishra. wrote:
swati mittal wrote:thanks to both of you.

its working fine.
But My application is already using some other function at body onload so how i can use this one.
because there will be only one "onload"


you can have it semicolon separated....like this

<body onload="setFocusOnLoad();ajax(page,'scriptoutput');">

Mansi


Thanks mansi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could I make a automatic call for ajax method
 
Similar Threads
Tiles Refresh!!!
Refresh page programatically
JSF 2.0 Changing CSS Dynamically
Realtime Applications
Job interview (Java developer)