Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-31-2007, 09:06 PM   PM User | #1
sports528
New Coder

 
Join Date: Oct 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
sports528 is an unknown quantity at this point
Js/AJAX getting server date not working in IE (php not supported)

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

Code:
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
Code:
<!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.
sports528 is offline   Reply With Quote
Old 11-01-2007, 02:34 PM   PM User | #2
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
May I ask why are you using Ajax to display info on the current page? That seems to be a little strange.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 11-01-2007, 10:18 PM   PM User | #3
sports528
New Coder

 
Join Date: Oct 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
sports528 is an unknown quantity at this point
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.)
sports528 is offline   Reply With Quote
Old 11-02-2007, 02:01 AM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-02-2007, 08:53 PM   PM User | #5
sports528
New Coder

 
Join Date: Oct 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
sports528 is an unknown quantity at this point
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
Code:
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");
}
sports528 is offline   Reply With Quote
Old 09-16-2009, 03:24 PM   PM User | #6
vikashdwi
New to the CF scene

 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
vikashdwi is an unknown quantity at this point
hello my autosuggest is not working on server even is working fine on localhost

<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>
vikashdwi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:06 PM.


Advertisement
Log in to turn off these ads.