Paste (MSDN): client-side scripting via XMLHTTP

Source: Internet
Author: User
Tags object comments contains html page include version xsl xsl file
xml| Script | client

Client-side scripting via XMLHTTP

Have you encountered any problems or challenges with the Internet or windows-based development? At this point, you can turn to Dr. GUI (drgui@microsoft.com), who will be visiting MSDN two times a month to answer your questions online.

Although the doctor's busy schedule makes it impossible for him to reply to all the questions, he will try to answer more questions here as much as possible. If your problem is selected, the doctor will send you a GUI doctor T-shirt!

Summary: Describes ways to create Web pages using client script, even with the system's XMLHTTP objects, XML files, and XSL style sheets to include formatting and scripting.

Dr. GUI ' s Bits and Bytes

This is the title of Dr. GUI's Web Diary. Unfortunately, the doctor has not added more content to this column recently. It may be because there is too much rain in Seattle, or if the weather is too cold, or if the sun is too strong, or it is delayed by something.

But you might like to read the story of Alice in Blibbetland there. Or a long-winded Daner Iloo.

In short, if you have something to say, please post your comments in the comments section of the day. or write ane-mail to Dr. GUI ...

Now, start to answer your questions!

To create a XMLHTTP with JScript HTML

Dear GUI DR:

I have a question about XML. I publish an ASP page through XMLHTTP, send the data back as HTML, and execute object.innerhtml on the div, table cell, or any element.

The problem I encountered was that once I created a form on the ASP Server page and sent it back in HTML, doing everything right. But I also want to do client-side validation on this page, so I put a JScript page link in the body of the response HTML I'm going to send back. After doing this, the script will not run.

When I include the actual JScript src reference on the parent page, it works correctly. Is this because JScript code is not loaded into the Run-time library? Can I not return the JScript code and make it work correctly in response to XMLHTTP post? Can you generate a JScript function for the page you create instantly?

I have not tried to do this in a different way: by XMLHTTP the XML data, and then sending it back as XML data, and then using the XSL stylesheet for conversion. I suspect that the JScript function can be defined in that way. But this approach seems too complicated. Do you have any suggestions or answers?

Thank you

Peter Sung

Dr GUI's reply:

Yes, Peter, you can return Microsoft JScript code in response to XMLHTTP Post/get and make the code executable. It's like getting a piece of cake first and then eating it. As you can guess, the best way to do this is to take advantage of the XSL style sheet, which is easy once you have all the types back to normal. The GUI doctor can even provide a quick example. (Of course, there are other solutions as well.) As long as the response contains the correct headers and characters in the correct order, the browser will interpret the response so that the response is working correctly. If necessary, you can generate a response with a string connection. )

In the following example, Dr. GUI is used in the ASP page for client code and VBScript (Microsoft Visual Basic scripting version). The strategy is this: the XML file contains only data (and references to style sheets). The XSL file contains all the HTML formats that are necessary to make up the full page, including the script located in the appropriate location. ASP pages consist primarily of calls to create a XMLHTTP object, load an XML file into the object (using an implicit style sheet), and write the resulting HTML to the response stream.

To try this method, follow the simple steps shown below. Copy and paste with Notepad, and save all files in C:\inetpub\wwwroot.

1.
Create a file that contains the following XML text and is named Books.xml. Note the reference to the Books.xsl style sheet. In addition, this file contains only data.

<?xml version= "1.0"?>
<?xml-stylesheet type= "text/xsl" href= "Books.xsl"?>
<catalog>
<book id= "bk101" >
<author>gambardella, matthew</author>
<title>xml Developer ' s guide</title>
</book>
<book id= "bk102" >
<author>ralls, kim</author>
<title>midnight rain</title>
</book>
</catalog>


2.
Then, create a file that contains the following code and is named Books.xsl. This file is the style sheet referenced by the books.xml created in step 1th. It contains the information necessary to correctly format the above XML file as an HTML page, even including scripts on that HTML page.

<?xml version= "1.0" encoding= "UTF-8"?>
<xsl:stylesheet version= "1.0"
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform">
<xsl:output method= "html" indent= "yes"/>
<xsl:template match= "/" >
<title>Test</title>
<script language= "JScript" >
function Test_onload ()
{
var objxml = new ActiveXObject ("MSXML2. DOMDocument ");
Objxml.async=false;
Objxml.load ("books.xml");
alert (objxml.xml);
Objxml=null;
}
</script>
<body >
<table border= "1" >
<xsl:for-each select= "Catalog/book" >
<tr>
<td>
<xsl:value-of select= "Author"/>
</td>
<td>
<xsl:value-of select= "title"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</xsl:template>
</xsl:stylesheet>


3.
Finally, create a file that contains the following code and is named Books.asp. This file is used only to create the XMLHTTP object, load the XML file for it, and write the resulting HTML to the response stream.

<%
Dim sxh
Response.ContentType = "Text/xml"
Set sxh = Server.CreateObject ("MSXML2. xmlhttp.3.0 ")
Sxh.open "Get", "http://localhost/books.xml", False
Sxh.send
Response.Write Sxh.responsetext
%>


4.
Open your browser and navigate to the Http://localhost/books.asp page.

5.
You will see an alert that contains books.xml data. This is done by the script in the XSL file. You'll also see neatly formatted data in a table on a Web page. This is done by the XSL code.

Thanks!

Dr. GUI is here to thank the excellent panel of experts, including Vijaya Byri and his special assistant Maura Baughman. The answer to the question will not be so satisfying if the GUI doctor does not have everyone's help in answering the question.

Consult the GUI doctor

The renowned problem-solving expert, Dr. GUI, is pleased to provide an encyclopedic knowledge of the Internet and windows-based development that benefits developers everywhere. If you have problems that cannot be resolved, please send your questions to drgui@microsoft.com. Although the doctor's busy schedule makes it impossible for him to reply to all the questions, he will try to answer more questions here as much as possible. If your problem is selected, the doctor will send you a GUI doctor T-shirt! (Please note: The problem may be sorted to ensure that the syntax is correct and the logic is clear.) )

Don't you have enough knowledge from Dr. GUI? Please read the GUI. NET Dr Column!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.