Wednesday 5 March 2008

Browsers comparison: read external file/data

1

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");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e) {alert(e.message)}
}
try
{
try{// with this config, firefox can retrive remote xml file.
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}catch(e){}
xmlDoc.async=false;
var address = "http://crossonline.spaces.live.com/feed.rss";
alert(address);
xmlDoc.load(address);
//xmlDoc.load("1.rss");
}
catch(e) {alert(e.toString())}

try{
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(desc);
alert(l);

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

COmparison:

  • 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: "
      <localhost>

      [Error:
      name: Error
      message: Security violation
      ]".
  • Safari:
    • Both NOT, saying:
      Java Script

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

Conclusion:
1. Security and convenience can not exist together.
2.
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
twitter.com/zhengxin

Facebook & Twitter