Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 9th, 2004, 05:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default XML & Netscape

Dear All,
    I'm using the following code in my webpage to load an XML file into a set of arrays - for use by another JS Application - but for some reason in Netscape it fails to find any elements in the XML file,
the code is :

Code:
var text = new Array();
var header = new Array();
var linka = new Array();
var targa = new Array();
var newsItems;
var xmldoc;

// for DOM2 including NN 6 & 7
if (document.implementation && document.implementation.createDocument)
{
    // create the XML Doc
    xmldoc = document.implementation.createDocument("", "", null);

    // define a function to run on load
    xmldoc.onload=function(){pop_ticker();}
}

// for IE
else if (window.ActiveXObject)
{
    // create a new XML object for IE
    xmldoc=new ActiveXObject("Microsoft.XMLDOM");

    // define some attributes of the doc
    xmldoc.async = false;

    // define a function to run on load
    xmldoc.onreadystatechange=function(){if(xmldoc.readyState == 4){pop_ticker();}}
}

// for the rest
else
{
    alert('Your browser can\'t handle this script');
}

//load the XML file in
xmldoc.load('news.xml');

// this is the function that populates the arryas from the XML so that the ticker will work
function pop_ticker()
{
    // load the news items from the XML file
    newsItems = xmldoc.getElementsByTagName("newselement");

    // populate the Arrays from the XML file
    for (var i=0;i<newsItems.length;i++)
    {
        // add the elements of the ticker item
        text[i]=newsItems[i].getElementsByTagName("headline")[0].firstChild.data;
        header[i]=newsItems[i].getElementsByTagName("newsitem")[0].firstChild.data;
        linka[i]=newsItems[i].getElementsByTagName("newslink")[0].firstChild.data;
        targa[i]=newsItems[i].getElementsByTagName("newstarget")[0].firstChild.data;

        if (targa[i]=="a")
        {
            targa[i]="";
        }
    }
}
and a snippet of the XML file is :

Code:
<newsticker>
    <newselement>
        <headline>Stop invalid characters in Object Names</headline>
        <newsitem>New Customisation</newsitem>
        <newslink>customisations/fortyeight/</newslink>
        <newstarget>a</newstarget>
    </newselement>
    <newselement>
        <headline>DB Upgrade Status bug fix</headline>
        <newsitem>New Customisation</newsitem>
        <newslink>customisations/fortyseven/</newslink>
        <newstarget>a</newstarget>
    </newselement>
</newsticker>
any ideas ?
__________________
All the best

Greg
------------------
Greg Griffiths
Web & Livelink Developer
http://www.greggriffiths.org
 
Old February 10th, 2004, 09:51 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You posted a similar question before and I said that I thought it was because Netscape always loads asynchronously and that the document hadn't loaded when you tried to query it. When pop_ticker runs can you alert the contents of the xml document to make sure it is available? I don't have NN on this machine to test myself.



--

Joe
 
Old February 10th, 2004, 07:54 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

When I do an :

alert(xmldoc)

as the first line in the popticker function, NN7.1 returns :

[ObjectXMLDocument]

does that help ?
 
Old February 11th, 2004, 11:58 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

What does alert(xmldoc.xml) show?



--

Joe
 
Old February 11th, 2004, 07:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

It returns :

undefined
 
Old February 12th, 2004, 09:56 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

As I am on a temporary machine I don't have Netscape but I will investigate further when I get home. Presumably the xml property does not exist in the Netscape xml document model.

Good luck.

--

Joe
 
Old February 12th, 2004, 06:34 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

While the command not being supported is possible, I've rechecked the code from the article I got it from - http://builder.com.com/5100-6389_14-5109488.html - and can' spot anything different, they even have a working demo - http://img.com.com/i/tr/bldr/Ticker/exampleD.html.
 
Old February 15th, 2004, 09:50 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

After installing latest version of Mozilla I managed to test script below. Works fine. Does JavaScript console show any messages whilst running script?
Code:
<script type="text/javascript">
var text = new Array();
var header = new Array();
var linka = new Array();
var targa = new Array();
var newsItems;
var xmldoc;

// for DOM2 including NN 6 & 7
if (document.implementation && document.implementation.createDocument)
{

    // create the XML Doc
    xmldoc = document.implementation.createDocument("", "", null);

    // define a function to run on load
    xmldoc.onload=function(){pop_ticker();}
    Node.prototype.__defineGetter__("xml", _Node_getXML);
}

// for IE
else if (window.ActiveXObject)
{
    // create a new XML object for IE
    xmldoc=new ActiveXObject("Microsoft.XMLDOM");

    // define some attributes of the doc
    xmldoc.async = false;

    // define a function to run on load
    xmldoc.onreadystatechange=function(){if(xmldoc.readyState == 4){pop_ticker();}}

}

// for the rest
else
{
    alert('Your browser can\'t handle this script');
}

function _Node_getXML()
 {

    //create a new XMLSerializer
    var objXMLSerializer = new XMLSerializer;

    //get the XML string
    var strXML = objXMLSerializer.serializeToString(this);

    //return the XML string
    return strXML;
}

//load the XML file in
xmldoc.load('news.xml');

// this is the function that populates the arryas from the XML so that the ticker will work
function pop_ticker()
{
    alert(xmldoc.xml);
    // load the news items from the XML file
    newsItems = xmldoc.getElementsByTagName("newselement");

    // populate the Arrays from the XML file
    for (var i=0;i<newsItems.length;i++)
    {
        // add the elements of the ticker item
        text[i]=newsItems[i].getElementsByTagName("headline")[0].firstChild.data;
        header[i]=newsItems[i].getElementsByTagName("newsitem")[0].firstChild.data;
        linka[i]=newsItems[i].getElementsByTagName("newslink")[0].firstChild.data;
        targa[i]=newsItems[i].getElementsByTagName("newstarget")[0].firstChild.data;

        if (targa[i]=="a")
        {
            targa[i]="";
        }
        alert(text[i]);
    }
}


</script>
--

Joe
 
Old February 19th, 2004, 07:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

no change unfortunately.
 
Old February 20th, 2004, 07:08 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well it works fine for me on two machines. I read a post in google.groups that said that the xml features were unreliable in the earlier versions. What version are you running? Does the JavaScript console show any messages?

--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML Javascript + Netscape equivalent sasidhar79 XML 0 October 19th, 2004 02:49 AM
XML from a DB recordset (removal of &lt;&gt;) Thodoris XML 3 July 13th, 2004 12:28 AM
Disabling copy/paste in both IE & Netscape Mekala HTML Code Clinic 0 July 2nd, 2004 04:24 AM
XML and netscape / mozilla madhukp XML 1 May 21st, 2004 04:01 PM
XML and netscape / Mozilla madhukp Classic ASP XML 0 May 3rd, 2004 04:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.