Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could I make a automatic call for ajax method

 
swati mittal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Swati

Please check this...

http://www.neowin.net/forum/lofiversion/index.php/t447063.html
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to use AJAX for that? Just client-side JavaScript should be sufficient for that, no?
 
swati mittal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Ulf Dittmer

Please tell me how i can use javascript to refresh particular part of web page.
 
Mansi Mishra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ lin qun

How i will call this javascript function.
Because I want to display date and time without clicking any where.
 
lin qun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic