On Wed, Mar 02, 2005 at 05:16:54PM +0800, Miguel A Paraz wrote:
> The XML DOM handling is also different from Mozilla and IE. Thus, the
> need for an IE testbed (to swing back to my original topic)

Well, not really that I've seen.  I wrote a JS search application for a
DVD project for a client, and the only differences between the browsers
was the object instantiation.

There are potential problems, but for once Microsoft isn't at the center
of them.  First, forget Opera.  No biggy to me- I can't understand why
anybody would use that browser now, anyway.  All but the most recent
version of Safari won't work, either.  And it gets worse- Safari
*claims* to have the capability but it does nothing.  And, IE for Mac
won't do it- ever.  Again, why anybody would use IE for Mac is so far
beyond my comprehension that I can't even see it from here.

For local stuff, I use synchronous reads.  That also greatly simplifies
the coding.  If a file is inaccessible, the browser locks up, so it's
better to use asynchronous reads with a timeout if going over the
internet.

Here is my code for reading a file synchronously:

/* GPL License - Copyright 2005 Michael Chaney Consulting Corporation */
/* Note that this script relies on having a file called "0.xml" to test
Microsoft's DOM loading capabilities.  Search for 0.xml to change it to
something else. */

// Constants
var using_async_calls=false;

var show_debug = false;

// Microsoft keeps changing the name of this, so we'll try them in order
// Based on code at
// 
http://jpspan.sourceforge.net/wiki/doku.php?id=javascript:xmlhttprequest:snippets:msxmlhttp
var ms_xml_objects = new Array(
                'Microsoft.XMLHTTP',
                'MSXML2.XMLHTTP.5.0',
                'MSXML2.XMLHTTP.4.0',
                'MSXML2.XMLHTTP.3.0',
                'MSXML2.XMLHTTP'
);

var ms_dom_objects = new Array(
                'Microsoft.XMLDOM'
);

// State variables
var results    = null;
var search_err = "";
var search_method = 'unknown';
var search_object_type = 'unknown'; // xmlhttp or dom
var search_activex_object = null;

/*
 From David Flanagan's, _Javascript:_The Definitive_Guide_, pg. 294-5,
  published by O'Reilly, 4th edition, 2002
*/
function debug(msg) {
        if (!show_debug) return; // Disable debugging

        if(!debug.box) {
                debug.box = document.createElement("div");
                debug.box.setAttribute("style",
                                "background-color:white" + 
                                "font-family: monospace; " +
                                "border: solid black 3px; "+
                                "padding: 10px;");

                document.body.appendChild(debug.box);
                debug.box.innerHTML = "<h1 style='test-align:cent'>Debugging 
Output</h1>";
        }

        msg.replace(/&/g,"&amp;");
        msg.replace(/</g,"&lt;");
        msg.replace(/>/g,"&gt;");

        var p = document.createElement("p");
        p.appendChild(document.createTextNode(msg));
        debug.box.appendChild(p);
}

// Find out if we can search
function canSearch() {

        if (search_method!='unknown') return search_method;

        // Safari 1.0 lies about its capabilities.  We must have Safari >=1.2.3
        // to do this search.  http://www.quirksmode.org/js/detect.html is a
        // good page dealing with detection.  We need to look at
        // navigator.userAgent, which ends with "Safari/125.9".  This is
        // Safari 1.2.3, not sure if the "125.9" is directly related.

        var res=navigator.userAgent.match(/Safari\/(\d+)\.(\d+)?/);
        if (res != null) {
                if (parseInt(res[1])<125 || (res[1]=='125' && 
parseInt(res[2])<9)) {
                        search_method=false;
                        return false;
                }
        }
        if (window.XMLHttpRequest) {
                search_method='XMLHttpRequest';
                search_object_type='xmlhttp';
        } else if (document.implementation && 
document.implementation.createDocument) {
                search_method='document.implementation';
                search_object_type='dom';
        }
        // The MS XMLHTTP object has a really stupid bug where a local
        // synchronous read will work, but the responseXML property will be
        // null.  The workaround is to use their XMLDOM object instead.
        // It'll use the same methods (load instead of send) as the
        // doc.implementation method.  We'll try to load 0.xml here and
        // revert to the dom method if it doesn't work.  I'm going to this
        // trouble because a) I believe they'll fix the xmlhttp object
        // someday and b) I believe the xmlhttp object is the future.
        else if (window.ActiveXObject) {
                var xmldoc=null;
                for (var i=0 ; i<ms_xml_objects.length ; i++) {
                        try {
                                xmldoc = new ActiveXObject(ms_xml_objects[i]);
                                search_method='msactivex.xmlhttp';
                                search_activex_object=ms_xml_objects[i];
                                search_object_type='xmlhttp';
                                break;
                        }
                        catch (e) {
                                // No!
                        }
                }
                // We'll test it with 0.xml to see if it actually works.  Note
                // that the xml doc has already been created.
                if (search_method!='unknown' && xmldoc) {
                        xmldoc.open("GET", "0.xml", false);
                        xmldoc.send();
                        if 
(xmldoc.responseXML.getElementsByTagName("k").length==0) {
                                search_method="unknown";
                                search_activex_object="";
                                search_object_type="";
                                debug("canSearch sees that Microsoft's xmlhttp 
object can't load a local file synchronously");
                        }
                        xmldoc=null;
                }
                // Try the DOM method otherwise
                if (search_method=='unknown') {
                        for (var i=0 ; i<ms_dom_objects.length ; i++) {
                                try {
                                        xmldoc = new 
ActiveXObject(ms_dom_objects[i]);
                                        search_method='msactivex.xmldom';
                                        search_activex_object=ms_dom_objects[i];
                                        search_object_type='dom';
                                        break;
                                }
                                catch (e) {
                                        // No!
                                }
                        }
                }
        } else {
                search_method=false;
        }
        debug("canSearch found "+search_method+" "+search_object_type+" 
"+search_activex_object);
        return search_method;
}

// This function loads the XML document from the specified URL.
// Timeouts are not good :)  Probably shouldn't be used over a network,
// works great locally.
function loadXML(url) {

        debug("loadXML("+url+")");

        // canSearch will set up the methods that we'll use here
        if (search_method=='unknown') {
                if (!canSearch) return false;
        }

        debug("loadXML will use "+search_method+" "+search_object_type+" 
"+search_activex_object);

        var xmldoc=null;

        try {
                // Create the object using the appropriate method
                switch (search_method) {
                        case 'XMLHttpRequest':
                                debug("XMLHttpRequest");
                                xmldoc=new XMLHttpRequest();
                                break;
                        case 'document.implementation':
                                debug("document.implementation");
                                xmldoc = 
document.implementation.createDocument("", "", null);
                                break;
                        case 'msactivex.xmlhttp':
                                debug("msactivex.xmlhttp");
                                xmldoc = new 
ActiveXObject(search_activex_object);
                                break;
                        case 'msactivex.xmldom':
                                debug("msactivex.xmldom");
                                xmldoc = new 
ActiveXObject(search_activex_object);
                                break;
                        default:
                                alert("Confusion");
                                break;
                }
        }
        catch (e) {
                alert("Your browser is incompatible with our search system.");
                return false;
        }

        if (!xmldoc) {
                alert("Your browser is incompatible with our search system.");
                return false;
        }

        debug("Object created...");

        try {
                switch (search_object_type) {
                        case 'xmlhttp':
                                xmldoc.open("GET", url, false);
                                xmldoc.send(null);
                                if (xmldoc.readyState==4) {
                                        
debug(xmldoc.responseText.slice(0,100)+(xmldoc.responseText.length>100?"...":""));
                                        return xmldoc.responseXML;
                                } else {
                                        return false;
                                }
                                break;
                        case 'dom':
                                xmldoc.async=false;
                                xmldoc.load(url);
                                return xmldoc;
                                break;
                        default:
                                alert("Confusion");
                                break;
                }
        }
        catch(ex) {

                // just return false here, as the file probably doesn't exist
                //search_err="An index file is missing: "+url;
                //return false;

                alert(ex);
                debug ("CAUGHT EXCEPTION!");
                return false;
        }

        return false;
}


With minor changes, that could be asynchronous reads.  You just need to
pass in a function to "loadXML" to execute when the XML is loaded, and
perhaps a timeout function, too.  The asynchronous read code that I have
is from the O'Reilly Javascript book and is really messy.  I wrote the
above from scratch.

The original project that this came from was written by a friend here in
town as a Javascript search application.

http://www.elucidsoft.net/projects/jsfind/index.html

I have a very updated version which Shawn hasn't had time to put up yet,
and if anyone's really interested I can forward it to them.  If you want
to see what it does, go here:

http://dvd.pointclassics.com/search.html

And do the search.  If you're clever, you can download the scripts from
there :)  It's set up to do synchronous reads, so don't do it with a
flakey connection or your browser may lock up.  And remember- there's no
search engine on the server, it's all in javascript.

There are lots of good references online, but here are some of my bookmarks:

Good cross-browser info:
http://www.howtocreate.co.uk/tutorials/jsexamples/importingXML.html
More:
http://www.quirksmode.org/js/detect.html
JSON- alternative to XML:
http://www.crockford.com/JSON/xml.html

Something fun: Lemmings in JS:
http://193.151.73.87/games/lemmings/

Michael
-- 
Michael Darrin Chaney
[EMAIL PROTECTED]
http://www.michaelchaney.com/
--
Philippine Linux Users' Group (PLUG) Mailing List
[email protected] (#PLUG @ irc.free.net.ph)
Official Website: http://plug.linux.org.ph
Searchable Archives: http://marc.free.net.ph
.
To leave, go to http://lists.q-linux.com/mailman/listinfo/plug
.
Are you a Linux newbie? To join the newbie list, go to
http://lists.q-linux.com/mailman/listinfo/ph-linux-newbie

Reply via email to