Solved

how to change it to AJAX

Posted on 2006-11-15
8
188 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
File names with & character 6 83
I starting with php 12 137
Re-position sub-options beneath the TAB 7 98
Cordova Camera plugin fails 2 141
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 …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

830 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