Solved

how to change it to AJAX

Posted on 2006-11-15
8
185 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now