Wednesday, 5 March 2008

Browsers comparison: read external file/data


Using following js code. In the html codes, one of script uses src= following js file, the other script calls the function defined in js file:

function ieparse(){
try //Internet Explorer
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
try //Firefox, Mozilla, Opera, etc.
catch(e) {alert(e.message)}
try{// with this config, firefox can retrive remote xml file."UniversalBrowserRead");
var address = "";
catch(e) {alert(e.toString())}

var xmlobject = xmlDoc;
// get a reference to the root-element "rss"
var root = xmlobject.getElementsByTagName('rss')[0];
// get reference to "channel" element
var channels = root.getElementsByTagName("channel");
// now get all "item" tags in the channel
var items = channels[0].getElementsByTagName("item");
// in the "item" we have a description, so get that
var descriptions = items[0].getElementsByTagName("description");
// we also get the "pubDate" element in the "item"
var links = items[0].getElementsByTagName("link");

// get the actual description as string
var desc = descriptions[0].firstChild.nodeValue;
// same for the pubDate
var l = links[0].firstChild.nodeValue;

// now we have the RSS data we want as strings - do something now :-)

document.write("xmlDoc is loaded, ready for use");
catch(e) {alert("wrong when processing the xmlstring")}


  • IE 7:
    • local file and remote file;
  • Firefox:
    • local file-work; remote file NOT,saying:
      [JavaScript Application]

      Permission denied to call method XMLDocument.load
  • Opera:
    • local file-work; remote file NOT,saying: "

      name: Error
      message: Security violation
  • Safari:
    • Both NOT, saying:
      Java Script

      TypeError: Value undefined (result of expression xmlDoc.load) is not object.

1. Security and convenience can not exist together.
new ActiveXObject("Microsoft.XMLDOM") is a little bit out of date, while XMLHttpRequest (for universal browser) and Msxml2.XMLHTTP (for IE) is much better, with XMLHttpRequest firefox and safari works, Opera seems not so universal. My another post is about XMLHttpRequest to solve such a calling remote XML file.

No comments:

My photo
London, United Kingdom

Facebook & Twitter