⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajax_example_suggest.asp@output=print

📁 W3Schools tutorial..web designing
💻 ASP@OUTPUT=PRINT
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>AJAX Suggest Example</title>
 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />

<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

<script src="clienthint.js" type="text/javascript"></script> 
</head>
<body>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

<h1>AJAX Suggest Example</h1>
<a href="ajax_serverscript.asp"><img border="0" src="../images/btn_previous.gif" width="100" height="20" alt="Previous" /></a>
<a href="ajax_source.asp"><img border="0" src="../images/btn_next.gif" width="100" height="20" alt="Next" /></a>
<hr />
<p class="intro">We have seen that AJAX can be used to create more interactive applications. </p>
<hr />
<h2>AJAX Suggest Example</h2>
<p>In the AJAX example below we will demonstrate how a web page can communicate 
with a web server online as a user enters data into a standard HTML form. </p>
<hr />
<h2>Type a Name in the Box Below</h2>
<form action=""> 
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p> 

<hr />
<h2>Example Explained - The HTML Form</h2>

<p>The form above has the following HTML code:</p>

<table class="ex" width="100%" border="1" id="table1"><tr><td>
<pre>&lt;form&gt; 
First Name:
&lt;input type=&quot;text&quot; id=&quot;txt1&quot;
onkeyup=&quot;showHint(this.value)&quot;&gt;
&lt;/form&gt;</pre>
<pre>&lt;p&gt;Suggestions: &lt;span id=&quot;txtHint&quot;&gt;&lt;/span&gt;&lt;/p&gt; </pre>
</td></tr></table>

<p>
As you can see it is just a simple HTML form with an input 
field called &quot;txt1&quot;.</p>
<p>
An event attribute for the input field defines a function to be triggered by the 
onkeyup event. </p>
<p>The paragraph below the form contains a span called &quot;txtHint&quot;. The span is 
used as a placeholder for data retrieved from the web server.</p>
<p>When the user inputs data, a function called &quot;showHint()&quot; is executed. The 
execution of the function is triggered by the &quot;onkeyup&quot; event. In other words: 
Each time the user moves his finger away from a keyboard key inside the input 
field, the function showHint is called.</p> 

<hr />
<h2>Example Explained - The showHint() Function</h2>

<p>The showHint() function is a very simple JavaScript function placed in the 
&lt;head&gt; section of the HTML page.</p>
<p>The function contains the following code:</p>

<table class="ex" width="100%" border="1" id="table2"><tr><td>
<pre>function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById(&quot;txtHint&quot;).innerHTML=&quot;&quot;;
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert (&quot;Your browser does not support AJAX!&quot;);
  return;
  } 
var url=&quot;gethint.asp&quot;;
url=url+&quot;?q=&quot;+str;
url=url+&quot;&amp;sid=&quot;+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open(&quot;GET&quot;,url,true);
xmlHttp.send(null);
}</pre>
</td></tr></table>

<p>The function executes every time a character is entered in the input field.</p>
<p>If there is some input in the text field (str.length &gt; 0) the function 
executes the following:</p>
<ul>
	<li>Defines the url (filename) to send to the server</li>
	<li>Adds a parameter (q) to the url with the content of the input field </li>
	<li>Adds a random number to prevent the server from using a cached file</li>
	<li>Creates an XMLHTTP object, and tells the object to execute a function 
	called stateChanged when 
	a change is triggered</li>
	<li>Opens the XMLHTTP object with the given url.</li>
	<li>Sends an HTTP request to the server</li>
</ul>

<p>If the input field is empty, the function simply clears the content of the txtHint 
placeholder.</p>

<hr />
<h2>Example Explained - The GetXmlHttpObject() Function</h2>

<p>The example above calls a function called GetXmlHttpObject().</p>
<p>The purpose of the function is to solve the problem of creating different 
XMLHTTP objects for different browsers.&nbsp; </p>
<p>The function is listed below:</p>

<table class="ex" width="100%" border="1" id="table4"><tr><td>
<pre>function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);
    }
  }
return xmlHttp;
}</pre>
</td></tr></table>
<br />
<hr />
<h2>Example Explained - The stateChanged() Function</h2>

<p>The stateChanged() function contains the following code:</p>

<table class="ex" width="100%" border="1" id="table3"><tr><td>
<pre>function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById(&quot;txtHint&quot;).innerHTML=xmlHttp.responseText;
}
}</pre>
</td></tr></table>

<p>The stateChanged() function executes every time the state of the XMLHTTP 
object changes.</p>
<p>When the state changes to 4 (&quot;complete&quot;), the content of the txtHint 
placeholder is filled with the response text.</p>
<hr />

<a href="ajax_serverscript.asp"><img border="0" src="../images/btn_previous.gif" width="100" height="20" alt="Previous" /></a>
<a href="ajax_source.asp"><img border="0" src="../images/btn_next.gif" width="100" height="20" alt="Next" /></a>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -