SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    Baia Blu (La Spezia) - Italy
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML, Javascript and cross-browser compatibility

    Hi!
    I'm trying to read an XML file with Javascript client-side and I have some cross-browser compatibitity problems.

    If I build a xml object with

    var objXML = new ActiveXObject("Msxml2.DOMDocument.4.0");

    I read my nodes, I display them in a div and I load my page with Internet Explorer everything runs well but if I open that page with Mozilla, Opera or Netscape that object isn't created and obviously no result appear on my page.

    Do you know a way to read an xml file with javascript client-side that runs well with non-ie browsers?

    Thanks!

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    ActiveX is a Microsoft technology, so I wouldn't expect it to work in a browser other than IE/Windows. Try Creating a cross-browser wrapper for your XML functions. Also, there is currently no way to load XML in Opera or Konqueror/Safari, so be sure of your audience if you plan on using XML like that, or do the transformations on the server-side.

  3. #3
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gazingus offers an XML Parser written in JavaScript. A disclaimer is attached saying that the project was more of a learning experience for the author rather than a genuine solution, but it might help you.

    This is the only link I have in my bookmarks of such a parser, but I have seen others around, perhaps more complete than this one. All just a Google away.
    Of course, that's just my opinion. I could be wrong.

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Don't use XML, use JS and import that instead. Why translate a document when you can just import a js which will be read automatically.

    I created this....
    Code:
    function changeScript(newJScript) {
     oldScript=document.getElementsByTagName("script")[0]
     var newEl = document.createElement('script');
     newEl.setAttribute('type', 'text/javascript');
     newEl.setAttribute('src', newJScript);
     oldScript.parentNode.replaceChild(newEl,oldScript);
     changeScript.el = newEl;
     return true;
     }
    I also use a setTimeout to check a variable has changed
    Code:
    function detectLoad() {
    if(rhythmName==prevRhythmName) { setTimeout("detectLoad()",100) } else {ChangeRhythmDetails();changeRhythm(); }}
    A very useful script, works on all my browsers.......
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  5. #5
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    Also, there is currently no way to load XML in Opera or Konqueror/Safari,
    vgarcia, does my script above work in Konqueror/Safari? I'm interested to see how cross browser it is
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    Baia Blu (La Spezia) - Italy
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow!
    Thanks to everyone has answered to my question!
    There are very interesting alternates but I think I'll work on the vgarcia's one.
    I've already changed a little part of my script and now it runs well under mozilla and netscape. as concern other browsers I'll found a server-side solution.

    Thanks to Mr. Brownstone, that's a good parser but it too difficult to arrange to my aims :-)
    And thanks to Markdidj, it's a good solution but it isn't what I'm looking for. I create and manipulate the xml file with asp and I display real time changes with js, it's more easy for me

    Regards

  7. #7
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Alethesnake
    And thanks to Markdidj, it's a good solution but it isn't what I'm looking for. I create and manipulate the xml file with asp and I display real time changes with js, it's more easy for me
    Just letting you know that creating and manipulating external js files can also be done server side. I'm using perl for mine, and I found it quite easy.

    my js files look something like this....
    Code:
    array[0]="blah,blah,moreblah"
    array[1]="waffle,waffle,waffle"
    array[2]="snore,snore,bored"
    array[3]="not,much,else,to,think,of,so,early,in,the,morning"
    so when I read it server side, the line number equals the array number.
    To add a new line I get the line count, and that is the number for my new array. You (or others) may find a use for it.......

    I think a good use for this method would be sharing dynamic data between sites.

    Good luck

    Added: This method also allows for refreshing external js files that may have been changed, as it replaces the cached file.
    Last edited by Markdidj; Jan 2, 2004 at 21:06.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    Baia Blu (La Spezia) - Italy
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it's a good solution, and the structure you have done to the js file is very interesting but at the moment all my asp scripts use XMLDOM instead of FileSystemObject, so for me it's easier to change file visualization wth js than every other thing.
    Thanks a lot for your help, it give me a good idea for a chat

    Bye

  9. #9
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks Alethesnake.

    I've used it for my page counter and my first forum.
    I know search engines won't pick it it, but it's not really needed.

    If anyone wants to use it for forum or chat boxes its really simple to use, and good for sites with low bandwidth ( javascript gets used from cache client-side unless changed )

    My forum HTML page stays the same. The page reads the messageCount.js and finds the total number of messages for that forum. I then use this to check the messages wishing to be read, as the messages are saved in groups of 10. (index0.js, index1.js, index2.js etc )
    Code:
    if(top.location.href.indexOf("?")>0) { messagePageNo=(top.location.href.split("?")[1]); } else messagePageNo=Math.floor(totalMessageCount/10);
    I've aslo started practising my regexp to check the messages,
    Code:
    function testMess(Obj) { 
    Obj.value=Obj.value.replace(/\<(\w+([\\\\\/\"&\s])|((script)|(img)|(.*frame)|(h1)))/g,'');
    document.getElementById("newMessPreview").innerHTML=Obj.value.replace(/\r/g,'<br>');}
     
    function preSendMess() {
    Obj=document.guestEntry.fGM;
    testMess(Obj);
    Obj.value=Obj.value.replace(/\!/g,'\\!');
    Obj.value=Obj.value.replace(/\"/g,'\\"');
    Obj.value=Obj.value.replace(/\%/g,'\\%');
    Obj.value=Obj.value.replace(/\&/g,'\\&');
    Obj.value=Obj.value.replace(/\?/g,'\\?');
    Obj.value=Obj.value.replace(/\@/g,'\\@');
    Obj.value=Obj.value.replace(/\\+/g,'\\\\\\');
    Obj.value=Obj.value.replace(/\r/g,'\<br>"+');
    Obj.value=Obj.value.replace(/\n/g,'\"');}
    test message (testMess(Obj)) checks that no attributes are set after a HTML tag and disallowes script,img,frames and H1 tags. The rest of preSendMess() enables escapement of characters through my perl and script.

    2 questions: Is testMess() sufficient to do its job, and is there a way I can condense preSendMess() ? It seems long winded....

    Something for the beetle probably......

    it's at my site http://www.unitingrhythms.co.uk/forums/guestbook/ if anyone wants to look and give opinions on safety....

    Added: Just been for a dog walk ( always good for inspiration (no :dog: but exists for the ladies?? )) and realised I don't actually use it for my forum, apart from checking if new messages exist (changeScript("messageCount")), but I am going to use it to display new messages in the future.....
    Last edited by Markdidj; Jan 3, 2004 at 09:53.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  10. #10
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Markdidj
    vgarcia, does my script above work in Konqueror/Safari? I'm interested to see how cross browser it is
    I'll find out the next time I boot into Linux and post here .


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •