...

View Full Version : Js/AJAX getting server date not working in IE (php not supported)



sports528
10-31-2007, 09:06 PM
On my server, php is not supported. So I have been trying to implement a util that uses the servers date and time to validate forms and such. I am using javascript but am having trouble with the server-side part.

I have tried in the following code, but it works only in Opera, Mozilla, Safari
In IE, i get nothing for 'st' and 'NaN' obviously for date


var xmlHttp;
function srvTime(){

try {
//Firefox, opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//Internet Explorer
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTP');
}
catch (err3) {
//AJAX not supported, use CPU time.
alert("AJAX not supported");
}
}
}

xmlHttp.open('GET',window.location.href.toString(),false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send('');
return xmlHttp.getResponseHeader("date");

}

var st = srvTime();
var date = new Date(st);

.html file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>

<script type="text/javascript" src="date.js">
</script>
</head>
<body>
<script>document.write(st);</script>
<p>converted date: </p>
<script>document.write(date);</script>
<p>cpudate: </p>
<script>document.write(Date());</script>
</body>
</html>

If i am unable to do it this way, is there a way to use perl with javascript.

A1ien51
11-01-2007, 02:34 PM
May I ask why are you using Ajax to display info on the current page? That seems to be a little strange.

Eric

sports528
11-01-2007, 10:18 PM
This information will not be displayed on the page. I am trying to use my server time, not the User's time (Date()) in javascript. These values will be used for expiration testing, and many different uses in forms. (Ex. birthdate drop-down option to be used that has the highest year to be 15 years ago.)

felgall
11-02-2007, 02:01 AM
So why not create the page using either server side includes or Perl or whatever server side language is supported. Ajax is supposed to be used to update a page after it finishes loading and anything it could do before the page loads can be done without it.

sports528
11-02-2007, 08:53 PM
Because I would like to have the time functions stored in an external .js source file that can be used on different pages. So i couldn't figure out how to use SSI. and with perl, or any other server-side program, an alternative would be to have the time generated to a file called datetime and have javascript read the time from the file, but I am not sure how to do that.

but I did get the http request to work in different platforms....the code is as follows: (Although I am not sure how accurate it is since i have been unable to test it)

located in .js file

var xmlHttp;
function srvTime(){

try {
//Firefox, opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//Internet Explorer
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (eerr3) {
//AJAX not supported, use CPU time.
alert("AJAX not supported");
}
}
}

xmlHttp.open('HEAD',window.location.href.toString(),false);
xmlHttp.setRequestHeader("Content-Type", "text/html");
xmlHttp.send('');
return xmlHttp.getResponseHeader("Date");
}

vikashdwi
09-16-2009, 03:24 PM
<script type="text/javascript" src="jquery.js"></script>
<script>
function suggest(inputString){
if(inputString.length == 0) {
$('#suggestions').fadeOut();
} else {
$('#country').addClass('load');
$.post("autosuggest_city.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').fadeIn();
$('#suggestionsList').html(data);
$('#country').removeClass('load');
}
});
}
}

function fill(thisValue) {
$('#country').val(thisValue);
setTimeout("$('#suggestions').fadeOut();", 10);
}
</script>

<style>
#result {
height:20px;
font-size:16px;
font-family:Arial, Helvetica, sans-serif;
color:#333;
padding:5px;
margin-bottom:10px;
background-color:#FFFF99;
}
#country{
padding:3px;
border:1px #CCC solid;
font-size:17px;
}
.suggestionsBox {
position: absolute;
left: 35px;
top:18px;
margin: 26px 0px 0px 0px;
width: 180px;
padding:0px;
background-color: #FCA500;
border-top: 3px solid #000;
color: #fff;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList ul li {
list-style:none;
margin: 0px;
padding: 6px;
border-bottom:1px dotted #666;
cursor: pointer;
}
.suggestionList ul li:hover {
background-color: #FC3;
color:#FCA500;
}
ul {
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
color:#FFF;
padding:0;
margin:0;
}

.load{
background-image:url(loader.gif);
background-position:right;
background-repeat:no-repeat;
}

#suggest {
position:relative;
}

</style>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum