Griefforyou [Original]
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>
</ Head>
<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.