Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Web Q&A: Auto Downloads, Accessing XML, Opening Multiple Windows, and More

MSDN Magazine
Auto Downloads, Accessing XML, Opening Multiple Windows, and More
Download the code for this article: Web0204.exe (36KB)
Q How can I make files automatically download from a Web site if that Web site requires you to enter a user name and password prior to receiving the response?

A You can specify what user name and password to use at a Web site in the URL as follows:
http://Username:Password@www.example.com/ 
Accomplishing this through the DOM is almost as easy. The Open method has optional parameters that let you specify user name and password:
HTTP.Open "GET", WScript.Arguments(0), False,"Username","Password"
                            
Documentation can be found at open Method (IXMLHTTPRequest).

Q I'm trying to access an XML file that lives on one server (MyServer1) from a single ASP file that's deployed to two different servers—MyServer1 and MyServer2. Both ASP pages are running with anonymous access and use a machine account which has privileges to see the XML file on MyServer1. The problem I've been running into is that MyServer1 can load the XML file, but MyServer2 fails on the load method.
      Here's a snippet of my code embedded in an ASP file:
set XMLDoc=createObject( "MSXML2.DOMDocument" ) XMLDoc.async=false
                                XMLDoc.load("http://MyServer1/website/mosedata.xml") 
When I type http://MyServer1/website/mosedata.xml in Microsoft® Internet Explorer, I am able to view the entire contents of the XML file.
      Can the load method load the XML file from a remote server? Are there any special permissions involved? The security settings on the XML file allow everyone full control.

A This is a frequently asked question. See the Knowledge Base article "Load Method Fails When Loading XML File Over HTTP". The resolution involves using the ServerXMLHTTP component to load the XML doc. As this documentation states, you have to use the SetProperty method of MSXML2.DOMDocument to set the ServerHTTPRequest property to True. Here, True indicates that you should use the server-safe ServerXMLHTTP component to load the document to the server. ServerXMLHTTP only supports synchronous loading; thus, the async property must be set to False when ServerHTTPRequest is set to True.

Q Using JScript®, I've implemented a DHTML behavior which is attached to several IMG elements on an HTML page via Cascading Style Sheets (CSS).
      The behavior's onclick handler opens a new window like this:
var oPopup = window.open("about:" + oht, "Code", strWindowSpecs
                                ); 
"oht" is a string containing the skeleton document, including a reference to a style sheet.
      Next, the innerHTML is set via a custom property set in the IMG element. When this script is run on any of the IMG objects on the page, and any window that was previously created by the behavior is closed before clicking another IMG, everything behaves as expected.
      But if a window created in this way is still open when another IMG is clicked, I get this runtime error:
Error: Unexpected call to method or property access. 
      The following happens on the line where the created window gets its innerHTML set:
oPopup.document.body.innerHTML = strHtml; 
Is this due to an access violation? What can I do to get around it?

A You can check to see if the popup exists and close it before opening the new one.
Var oPopup //declare outside of the function if (oPopup!= null) oPopup.close();
                                oPopup = window.open("about:" + oht, "Code", strWindowSpecs
                                ); 
      If you need to allow multiple instances to be open at the same time, set the window name to "" to solve the problem.

Q I have an ASP page that loads an XML document returned from a stored procedure. Some of the tags have attributes that contain the data I need to display:
<Match crc64="4A9FB6BC9817D532" Title="Big" Year="1988"
                                Lead="Tom Hanks" IsExact="true" /> 
I need the Title and Year attributes. Here is the code I am using:
Set objXML = Server.CreateObject("Microsoft.XMLDOM") objXML.async
                                = false objXML.load(XMLFilePath) Set oDVDMatch = DVDlist.getElementsByTagName("Match")
                                iMatchCount = CInt(oDVDMatch.length) Response.Write oDVDMatch.item(0).text 
A You can use getElementsByTagName for faster execution. selectSingleNode and selectNodes are powerful for XPath searches and conditional searches which cannot be done using getElementsByTagName. You just search by Tag name. But remember both methods return IXMLDOMNodeList.
      It is incorrect to use oDVDMatch.item(0) to find attributes. What are you expecting from item(0)? Is it first attribute or first child? Use either childNodes for children or attributes collection for attributes and then traverse from there.
      You could also do something like this:
<% Set objXML = Server.CreateObject("Microsoft.XMLDOM") objXML.async
                                = false objXML.load(XMLFilePath) set oDVDMatch = objXML.selectSingleNode("Match")
                                iMatchCount = CInt(oDVDMatch.length) response.write "Title = " & oDVDMatch.attributes.getNamedItem("Title").text
                                response.write ", Year = " & oDVDMatch.attributes.getNamedItem("Year").text
                                Set objXML = nothing Set oDVDMatch = nothing %> 
Q In my HTML table (see Figure 1) how do I get column three to be the exact same height (64 pixels) as the other columns? Even if I specify a td height of 40, it still wants to be taller.

A This is a common problem and has nothing to do with the table at all, but actually with the alignment of your images. You need to align them "absmiddle," otherwise they cause weird padding issues within the cell (<td>).
      Add the alignment in each <td> like so:
<td width="1%"> <img align="absmiddle" height="64"
                                alt="corner" src="01.gif" width="18"> </td>
                                <td width="1%"> <img align="absmiddle" height="64"
                                alt="logo" src="02.gif" width="152"> </td>
                                <td width="97%" bgcolor="#cccc99" align="middle">
                                Table: 90% | Cells 1, 2, &amp; 4:&nbsp;1% | This cell: 97% </td> <td
                                width="1%"> <img align="absmiddle" height="64"
                                alt="corner" src="04.gif" width="18"> </td>
                            
Q I want to use JScript to allow me to send arbitrary data out on the wire, as if I were using the Winsock Win32® APIs. Specifically, I want to send and receive XML to the IP address/port of my own choosing. Is there anything that will allow low-level network operations exposed via script?

A Try looking at XMLHTTP, which lets you send and receive over HTTP. If all you want is to send and receive from a specific URL, this might serve your purpose. There are also lots of docs and code samples on MSDN® Online.
      There is no control as such that exposes low-level network APIs to JScript, but try looking at WinInet, WinHTTP, and even ServerXMLHTTP if the other options don't work for you. You can also use JScript .NET and the System.Net and System.IO classes.

Q Normally the following link would cause a document attachment to open in a new window.
<A href="attachment.doc" target="_blank" >Download</A>
                            
Note that the attachment opens within Internet Explorer.
      How does Hotmail® cause Microsoft Word (and not Internet Explorer) to open in a separate window and then open the document attachment? Also note that there is no script command to open a new window.

A Open a Windows® explorer window and select Tools | Folder Options from the menu bar; then select the DOC extension and change the Open Action, removing the checkmark from the "Browse in same window" checkbox. Word documents will now open in their own Word window.
      Here are some related Knowledge Base articles: "Raise a 'File Download' Dialog Box for a Known MIME Type", and "Internet Explorer Prompts the User with Two Open or Save Dialog Boxes". Microsoft .NET code to "force" the file download is available as part of the code for this column at the link at the top of this article. It makes Word (and not Internet Explorer) open the Word document in a separate window just like Hotmail. In addition, the file gets downloaded on the client machine and then gets opened in Word.
      It is safer to let the user view the attachment by downloading it onto his machine rather than opening it from the server location itself. Also, you can do an additional security check in the download.aspx to see if the user is entitled to view the attachment.
      The anchor tag would be something like:
<a href="download.aspx?path=\\svrname\foldername\filename.ext>
                                Click to view attachment</a> 
      On the other hand, converting the physical path of the file to an HTTP URL mapped to the physical path to let the user view it as an HTTP URL makes the particular vroot susceptible to hacking attempts on other attachments. Thus, the following would open a file that's present on the specified maching running Internet Information Services (IIS).
<a href=https://IISHostName/VrootMappedToSvrName/foldername/filename.ext>
                                Click to view attachment></a> 
Once the user knows the vroot for the attachment, he can attempt to view other attachments. Unlike the download.aspx scenario, it's difficult to implement code to do a security and permissions check.

Q Is there a way to make two different submit buttons the same size? I'm not using a fixed-width font.

A You most certainly can make buttons the same size using any font that you want, and this works with other controls, too.
<input type="submit" name="idtest1" value="test1"
                                style="width:82px;height:20px"> <input type="submit" name="idtest2"
                                value="test ABC 2" style="width:82px;height:20px"> 
      CSS is really the only reliable way to size controls in HTML. Sometimes the width and height attributes work—like they do on images—but CSS is really the way to go. Of course, with CSS you can do it inline (as you just saw), within a <style> block, or in an external .css file. The two external choices are the best because then you can do something like this:
<style> .button { width:82px; height:20px } </style> •••
                                <input type="submit" name="idtest1" value="test1"
                                class="button" /> <input type="submit" name="idtest2"
                                value="test ABC 2" class="button" /> 
      Now, when you have a third button with text longer than 82px (or when localization makes the text much longer), all you need to do is change a single line, and all of the buttons you have created using that class will change.

Q I have some questions about best practices regarding the use of namespaces. Suppose I have a three-level object hierarchy. Let's say I have bookcases containing albums which contain photos. I could represent this using three different namespaces (see Figure 2).
      The weird thing about this hierarchy is how the photo object is denormalized and has references to the album that contains it. The elements can have the same name, so namespaces are used to disambiguate them. It seems to me that here the <namespace:tagname> is used to indicate a datatype which I think is independent and belongs in the schema definition.
      My questions are: is it common practice to use a separate namespace for each object, is it a good idea to denormalize XML data that way, and if there are references to other objects couldn't they be described as <photo:albumid> and <photo:albumname>?

A It's a common practice to use namespaces to resolve or avoid name conflicts; that's exactly what namespaces are designed for. However, using a separate namespace for each element is surely overkill. You might consider designing an XML Schema to model the bookcase, and another separate XML Schema to model all of your photo related elements.

Q I am looking at putting some error handling on messages returned from the failure of an updategram in a Web page and putting a more user-friendly message to it. Should I be looking to OLE DB documentation for the sort of message I should expect, and are there any other types of messages returned from the ISAPI ?
      

A There are both types of messages. Most of them will come back from SQL Server™ (OLE DB) though, but some are specific updategram error messages (identity columns errors, badly formatted updategrams, and so on).
      Also, you can get annotated schema error messages if the schema contains errors. In addition, the error message texts (at least the error messages in SQLXML) have varied slightly from one release to the next. There are also other error messages such as MSXML error messages that can occur if the updategram or annotated schema contains an XML error (such as the doc is not well-formed).
      One good option is to trap the error messages you expect, replace them with friendlier ones, and use a catch-all user message for all the other error cases.

Q I've been happily creating SOAP vnames with SQLXML 3.0, and it has just ocurred to me that I have no idea of the purpose of the Domain Name field. If the domain name is left unspecified, it defaults to the computer name. Presumably, I can set the Domain Name field to my Windows domain name, but I'm not sure I understand what it's actually for.

A The Domain Name field is the domain name through which your vname will be accessed using URLs. It defaults to the name of the computer for intranet scenarios which are the most common, but you can also set it to "www.mymachine.com" for Internet scenarios, so that access will be through http:// www.mymachine.com/myvdir/myvname?wsdl. For more information, see Programming Microsoft SQL Server 2000 with XML by Graeme Malcolm (Microsoft Press, 2001).

Q I need to create lots of domain-specific script files, and I'd like to have them use a common set of functions from a separate JScript file. Is there any simple way to do this? I understand VBScript has the ExecuteGlobal method that allows you to do this, but is there anything I can do in my script files to get all of them to use this common JScript file?

A There is no great way to do this with straight JScript, but one adequate method is shown in Figure 3.
      You can also use Windows Script Host (WSH) and create WSC objects that you can use to access normal methods and functions as properties and methods of the objects. This would allow for encapsulation. For more information on this, see Cutting Edge in the September 1999 issue of Microsoft Internet Developer.

Q Why doesn't the garbage collector release a reference to an ActiveX® object when the page is refreshed? I know JScript utilizes lazy garbage collection, but in this case memory allocated is never released when you refresh the page. Futhermore, when you refresh the page, memory comsumption grows because of the new ActiveX object that's created.

A The problem is that it's Internet Explorer that holds the reference to the function which in turn holds a reference to the ActiveX object that isn't being released.
      For performance reasons, Internet Explorer does not kill the DOM nodes until either you navigate to a different page or you close the browser. Refreshing the current page will not kill the nodes. The problem is not that the garbage collector is lazy—the problem is that it's not immune to circular reference chains which contain objects not created by the JScript engine. The browser object model is created by the browser, not by the language engine.

Got a question? Send questions and comments to webqa@microsoft.com.
Thanks to the following Microsoft developers for their technical expertise: Rhonda Bailey, Dan Battagin, Michael Brundage, JJ Brychell, Frank Chen, Dennis Clawson, Bruno Denuit, Bulent Elmaci, David Hyland, Pranav Kandula, Andreas Klein, Karuna Koneru, Eric Lippert, Graeme Malcolm, Rahul Pandit, Thane Peterson, Luis Gómez Sánchez, Joel Sparler, Brad Thompson, Peter Torr, Ross Webster, Rudiger Wenzel, Juanya Williams, Steven Wort, Vincent Yung, Nanshan Zeng.

From the April 2002 issue of MSDN Magazine

Show:
© 2015 Microsoft