Solved

how to change it to AJAX

Posted on 2006-11-15
8
187 Views
Last Modified: 2010-04-06
I have a form with an iframe that refreshes every 5 seconds. I don't want it to refresh everytime but I need the latest data.

Basically this is for a chat program done in ASP. The form chatmain.asp contains an iframe on top and a textarea and submit button at the bottom of the form. When we type something and click the submit, the message gets added to a text file. The iframe display the page chatscreen.asp which reads the textfile and displays. The chatscreen.asp refreshes each 5 seconds. I will provide the code for the page that refreshes. I don't want this page to refresh, but should show the latest data.  I heard that it can be done using AJAX and that is what I want. I want somebody to change this code to AJAX.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim File_name
File_name = Request.QueryString("sFname")
%>
<!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">
<head>
<meta http-equiv="refresh" content="5;URL=chatscreen.asp?sFname=<%=File_name%>" />
<title>Chat with Alpine Agent</title>
<style type="text/css">
<!--
.s1 {font-family: Arial; color:#333366; font-size:12px}

body {
      background-color: #FFFFFF;
}
-->
</style>

</head>

<body onload="self.location.href='#base'">
<p class="s1">
<%
dim objOpenFile, objFSO, strPath
strPath = Server.MapPath("./" & File_name & ".txt")
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set objOpenFile = objFSO.OpenTextFile(strPath, 1)
While not objOpenFile.AtEndOfStream
      Response.Write objOpenFile.ReadLine
      Response.Write("<br />")
Wend
%>
<a id="bottom" name="base"><font color="#FFFFFF">Show this</font></a>
</p>
</body>
</html>
0
Comment
Question by:CWS (haripriya)
  • 4
  • 2
8 Comments
 
LVL 12

Expert Comment

by:netsmithcentral
ID: 17951058
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim File_name
File_name = Request.QueryString("sFname")
%>
<!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">
<head>
<title>Chat with Alpine Agent</title>
<script type="text/javascript">
<!--
window.onload = updateChat;

function updateChat(){
 if (window.XMLHttpRequest){
  http_request = new XMLHttpRequest();
 }
 else if (window.ActiveXObject){
  try {
   http_request = new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e) {      
   try {
    http_request = new ActiveXObject('Microsoft.XMLHTTP');
   } catch (e) {}
  }
 }
 http_request.onreadystatechange = function() {
  if(http_request.readyState == 4){
   if(http_request.status != 200){
    if(http_request.status == 403) alert('Chat log could not be updated.   403: Acess is forbidden');;
    else if(http_request.status == 404) alert('Chat log could not be updated.  404: No record of your chat exists.');
    else alert('Chat log could not be updated.  '+http_request.status+': Unknown error.');
   }
   else {
    try{http_request.setRequestHeader('content-type','text/html;charset=ISO_8859-1:1987');}catch(e){}
    setTimeout('updateChat();', 5000);
    document.getElementById('chatlog').innerHTML = http_request.responseText;
   }
  }
 };
 http_request.open('GET', <%=File_name&".txt"%>, true);
 http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
 http_request.send(null);
}
//-->
</script>
<style type="text/css">
<!--
.s1 {font-family: Arial; color:#333366; font-size:12px}

body {
     background-color: #FFFFFF;
}
-->
</style>

</head>

<body onload="self.location.href='#base'">
<p class="s1" id="chatlog">
<%
dim objOpenFile, objFSO, strPath
strPath = Server.MapPath("./" & File_name & ".txt")
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set objOpenFile = objFSO.OpenTextFile(strPath, 1)
While not objOpenFile.AtEndOfStream
      Response.Write objOpenFile.ReadLine
      Response.Write("<br />")
Wend
%>
</p>
<a id="bottom" name="base"><font color="#FFFFFF">Show this</font></a>
</body>
</html>
0
 
LVL 16

Author Comment

by:CWS (haripriya)
ID: 17953972
Hello netsmithcentral,

Thanks for your prompt reply. Unfortunately the page is not showing the latest data.
0
 
LVL 16

Author Comment

by:CWS (haripriya)
ID: 17954018
I want the page to check for the latest data every 5 seconds without refreshing the page.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 11

Expert Comment

by:kelvinwkw
ID: 17954595
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim File_name
File_name = Request.QueryString("sFname")
%>
<!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">
<head>
<%randomize%>
<meta http-equiv="refresh" content="5;URL=chatscreen.asp?sFname=<%=File_name%>&dummy=<%=rnd*100%>" />
<title>Chat with Alpine Agent</title>
<style type="text/css">
<!--
.s1 {font-family: Arial; color:#333366; font-size:12px}

body {
     background-color: #FFFFFF;
}
-->
</style>

</head>

<body onload="self.location.href='#base'">
<p class="s1">
<%
dim objOpenFile, objFSO, strPath
strPath = Server.MapPath("./" & File_name & ".txt")
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set objOpenFile = objFSO.OpenTextFile(strPath, 1)
While not objOpenFile.AtEndOfStream
      Response.Write objOpenFile.ReadLine
      Response.Write("<br />")
Wend
%>
<a id="bottom" name="base"><font color="#FFFFFF">Show this</font></a>
</p>
</body>
</html>

how bout this?
0
 
LVL 16

Author Comment

by:CWS (haripriya)
ID: 17954625
kelvinwkw,

The page still refreshes.
0
 
LVL 11

Accepted Solution

by:
kelvinwkw earned 500 total points
ID: 17962198
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim File_name
File_name = Request.QueryString("sFname")
%>
<!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">
<head>
<title>Chat with Alpine Agent</title>
<script type="text/javascript">
<!--

function updateChat(){
 http_request = new ActiveXObject("Microsoft.XMLHTTP");
 http_request.open('GET', '<%=trim(File_name)%>.txt?dummy='+Math.random(), false);
 http_request.send();
 document.getElementById('chatlog').innerHTML = http_request.responseText;
 setTimeout("updateChat()", 5000);
}
//-->
</script>
<style type="text/css">
<!--
.s1 {font-family: Arial; color:#333366; font-size:12px}

body {
     background-color: #FFFFFF;
}
-->
</style>

</head>

<body onload="updateChat();">
<span class="s1" id="chatlog">
<%
dim objOpenFile, objFSO, strPath
strPath = Server.MapPath("./" & File_name & ".txt")
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
set objOpenFile = objFSO.OpenTextFile(strPath, 1)
While not objOpenFile.AtEndOfStream
      Response.Write objOpenFile.ReadLine
      Response.Write("<br />")
Wend
%>
</span>
<a id="bottom" name="base"><font color="#FFFFFF">Show this</font></a>
</body>
</html>


how bout this?
0
 
LVL 16

Author Comment

by:CWS (haripriya)
ID: 18156840
pinaldave,

Sorry for the delay.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to dynamically set the form action using jQuery.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question