Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

please help a newbie , an error in an xml javascript based Slideshow ! 1

Status
Not open for further replies.

Hirdyesh

Programmer
May 20, 2005
7
AU
Hi All ,
Well as the title suggests i am trying to implment a javascript based slideshow which reads of an xml file and displays the pictures the xml file also contains a link for each image element which is displayed when the user clicks ona particular image.The code was embedded in my aspx page but it does n't work , during my past uncountable attempts to view the script in Firefox or IE i get this error
Error: document.images.slide has no properties
Source File: Line: 95
I am just a newbie to javascripts and have very recently recognised the potential power of them . I would be very greatfull if someone could please help me fix this error as i can feel it that the code is not too far from what it's meant to accomplish as i have managed to read of my xml file and display the content as an html table and i have manged to display and animation based on some passign in some pictures' url and links as array to my functions. Any help would be very highly appreciated .

<script type="text/javascript" language="javascript" >
// JScript File
var xmlDoc ;
var slideimages=new Array(10);
var slidelinks=new Array(10);
var slideshowspeed=2000
var whichlink=0
var whichimage=0

function importXML()
{ alert('inside importXML 1');
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = createTable;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) createTable()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("Top_Animation.xml");
}

function createTable()
{
var x = xmlDoc.getElementsByTagName('Image');

for (i=0;i<x.length;i++)
{
for (j=0;j<x.childNodes.length;j++)
{
if (x.childNodes[j].nodeType != 1) continue;
if (j=0)
{
document.images.item['slide']= x.childNodes[j].firstChild.nodeValue
slideshowimages(x.childNodes[j].firstChild.nodeValue);

}
if (j=1)
{

slideshowlinks(x.childNodes[j].firstChild.nodeValue);

}

}

}

}
function gotoshow()
{ importXML();
if (!window.winslide||winslide.closed)
winslide=window.open(slidelinks[whichlink])
else
winslide.location=slidelinks[whichlink]
winslide.focus()
}
function slideshowimages()
{
for (i=0;i<slideshowimages.arguments.length;i++)
{
slideimages=new Image()
slideimages.src=slideshowimages.arguments
}
}

function slideshowlinks()
{
for (i=0;i<slideshowlinks.arguments.length;i++)
slidelinks=slideshowlinks.arguments
}
function slideit()
{
if (!document.images)
return
document.images.slide.src=slideimages[whichimage].src
whichlink=whichimage

if (whichimage<slideimages.length-1)
whichimage++
else
whichimage=0
setTimeout("slideit()",slideshowspeed)
}

slideit()

</script>
<body >
<a href="javascript:gotoshow();"><img alt="animated banner" src=" id="slide"/></a>

My xml file looks like
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Top_Animation.xsl"?>
<Images xmlns:xsi="xsi:schemaLocation=" <Image>
<URL>
</URL>
<Link>
</Link>
</Image>
<Image>
<URL>
</URL>
<Link>
</Link>
</Image>
<Image>
<URL>
</URL>
<Link>
</Link>
</Image>
<Image>
<URL>
</URL>
<Link>
</Link>
</Image>
<Image>
<URL>
</URL>
<Link>
</Link>
</Image>
</Images>

my xss file looks like
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Images" xmlns="" xmlns:xs=" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Images" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Image">
<xs:complexType>
<xs:sequence>
<xs:element name="URL" type="xs:string" minOccurs="0" />
<xs:element name="Link" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

My xsl file looks something like this
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="
<xsl:template match="/">
<html>
<body>
<!--
This is an XSLT template file. Fill in this area with the
XSL elements which will transform your XML to XHTML.
-->
<h2>My Images</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">URL</th>
<th align="left">Link</th>
</tr>
<xsl:for-each select="Images/Image">
<tr>
<td>
<xsl:value-of select="URL"/>
</td>
<td>
<xsl:value-of select="Link"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>


Thanxs in advance !
 

well, first things first...you call the "slideit()" function before you define the img tag id "slide"...

Code:
    else
        whichimage=0
        setTimeout("slideit()",slideshowspeed)
}

slideit() #why are you calling this?

    </script>
<body >

that's why you get the "document.images.slide has no properties" error.

that gets rid of that. now you have a new error:

slideimages[whichimage] has no properties.

it's initially looking for slideimages[0], and not finding it. that's because it's not set until createTable() function runs. there's a function that runs that, and that's importXML(), which is not called until function gotoshow() is called, and that is called when you click on 'animated banner'.

at least you have figured out why you got the initial error. you're calling it out of sequence. take it out, and no more problem. it will get called inside a function. since i can't load the XML content, you'll have to report the progress.

- g
 
Hi Spewn ,
Firstly thanks for writing to my post .It was great to recieve your reply as it clears out the error like u said in your post , but the code does n't do what it's meant to do ,Now i have added another method to try preload the images and when the body of the html is loaded i am trying to call importxml .this is just so inorder to make the animation going as soon as the page loads up and when the user clicks the image then only the new window starts with the link , but at the moment when i load this in the browser firstly my slide show does n't start up automatically like it's meant too , i know it gets the image data some how cause when i click on the non changing image from IE it starts a new window with the corresponding URL attached to the image from my xml file but in Firefox upon clicking on the image it starts anew window and shows me the second Image from the xml file .I have slightly updated my code ! here it is again .At teh bottom i am also attaching code from one of my other MAster pages where this slideshow script runs as well but there i was programatically feeding in the picture's URL and there corresponding link urls .Just in case if they help some how as i have been onto this problem for quite sometime and really need to get thsi sorted out .Any suggestions and tips would be greatly appreciated.

MY new code
<script type="text/javascript" language="javascript" >
// JScript File
var xmlDoc ;
var slideimages=new Array(10);
var slidelinks=new Array(10);
var slideshowspeed=2000
var whichlink=0
var whichimage=0

function importXML()
{ alert('inside importXML 1');
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = createTable;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) createTable()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("Top_Animation.xml");
}

function createTable()
{
var x = xmlDoc.getElementsByTagName('Image');

for (i=0;i<x.length;i++)
{
for (j=0;j<x.childNodes.length;j++)
{
if (x.childNodes[j].nodeType != 1) continue;
if (j=0)
{
MM_preloadImages(x.childNodes[j].firstChild.nodeValue);
document.images.item['slide']= x.childNodes[j].firstChild.nodeValue
slideshowimages(x.childNodes[j].firstChild.nodeValue);

}
if (j=1)
{

slideshowlinks(x.childNodes[j].firstChild.nodeValue);

}

}

}

}
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}
}
function gotoshow()
{
if (!window.winslide||winslide.closed)
winslide=window.open(slidelinks[whichlink])
else
winslide.location=slidelinks[whichlink]
winslide.focus()
}
function slideshowimages()
{
for (i=0;i<slideshowimages.arguments.length;i++)
{
slideimages=new Image()
slideimages.src=slideshowimages.arguments
}
}

function slideshowlinks()
{
for (i=0;i<slideshowlinks.arguments.length;i++)
slidelinks=slideshowlinks.arguments
}
function slideit()
{
if (!document.images)
return
document.images.slide.src=slideimages[whichimage].src
whichlink=whichimage

if (whichimage<slideimages.length-1)
whichimage++
else
whichimage=0
setTimeout("slideit()",slideshowspeed)
}

//slideit()

</script>
</head>
<body onload="importXML();">
<a href="javascript:gotoshow();"><img alt="animated banner" src=" id="slide"/></a>


my general Master page code //Note this one does n't load from any xml file just in case if it helps anyhow.

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
<!--


var slideimages=new Array()
var slidelinks=new Array()

function slideshowimages(){
for (i=0;i<slideshowimages.arguments.length;i++){
slideimages=new Image()
slideimages.src=slideshowimages.arguments
}
}

function slideshowlinks(){
for (i=0;i<slideshowlinks.arguments.length;i++)
slidelinks=slideshowlinks.arguments
}

function gotoshow(){
if (!window.winslide||winslide.closed)
winslide=window.open(slidelinks[whichlink])
else
winslide.location=slidelinks[whichlink]
winslide.focus()
}



//-->
</script>
<link rel="stylesheet" href="text.css" type="text/css"/>
<style type="text/css">

body {
background-color: #d5d5d5;
background-image: url(bkg.jpg);
background-repeat: repeat-x;
}

</style>
</head>
<body class="text" onLoad="MM_preloadImages(' alink="#484848" vlink="#484848" link="#CC0000">
<form id="form1" runat="server">
<div align="center">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="middle" align="left">
<table cellpadding="0" cellspacing="0" border="0" height="35">
<tr>
<td width="535"><!--THIS IS JUST THE "CLIPSTONE YAMAHA" TITLE--><img src="pixel.gif" alt="" width="22" height="1" border="0"><img src="title.gif" alt="" width="190" height="31" border="0"></td>
<td><!--THIS IS THE ADDRESS, ITS NOT AN IMAGE BUT RATHER ACTUAL TEXT--><font face="Verdana" size="1" color="#FFFFFF">53 Maroondah Hwy Ringwood 3134</font></td>
</tr>
</table>
</td>
</tr>
<tr>
<td><!--THIS IS THE MAIN MENU, U NEED TO ADD THE CODING FOR THE DROP DOWN MENU IN HERE OF COURSE, DON'T USE THE SPACE BAR THOUGH OR YOU WILL GET GAPS!--><img src="01.gif" alt="" width="19" height="32" border="0"><img src="02.gif" alt="" width="55" height="32" border="0"><img src="03.gif" alt="" width="82" height="32" border="0"><img src="04.gif" alt="" width="93" height="32" border="0"><img src="05.gif" alt="" width="81" height="32" border="0"><img src="06.gif" alt="" width="93" height="32" border="0"><img src="07.gif" alt="" width="114" height="32" border="0"><img src="08.gif" alt="" width="60" height="32" border="0"><img src="09.gif" alt="" width="54" height="32" border="0"><img src="10.gif" alt="" width="84" height="32" border="0"><img src="11.gif" alt="" width="18" height="32" border="0"></td>
</tr>
<tr><td height="10"></td></tr>
<tr><td>
<!--THIS WILL BE WHERE THE ANIMATED IMAGES ARE-->
<a href="javascript:gotoshow()"><img alt="animated banner" src=" id="slide"/></a>
<script>

//configure the paths of the images, plus corresponding target links
slideshowimages("slideshowlinks("var slideshowspeed=2000
var whichlink=0
var whichimage=0
//configure the speed of the slideshow, in miliseconds


function slideit(){
if (!document.images)
return
document.images.slide.src=slideimages[whichimage].src
whichlink=whichimage
if (whichimage<slideimages.length-1)
whichimage++
else
whichimage=0
setTimeout("slideit()",slideshowspeed)
}

slideit()


</script>

Thanxs in advance !
 
One more thing ,i missed on to inform you that both firefox and ie keep giving me a notification when i run the page that the script on the page is taking too long to execute and if I would like to abort running it as it may slow down my machine or mozilla firefox.i am guessing this is cause of loading of the pictures at runtime .but i am not sure could certainly be wrong ,Whadya say Mate?

 
Hi All, Well after not getting any help from anyone and at the same time trying to fix the script myself since my first post , I have finally manged to successfully make a javascript which reads of Picture Urls and links from a server side XMl file and then displays them in the browser ,I have it currently running in Both IE and Mozilla type browsers (Netscape, FireFox).The problem i found was that in Mozilla the index of the child nodes from the xml file are numbered in weird fashion .It would number the first node(Zeroth) as 3 instead of
So i had to make two different methods to load up the array Pix filled with all the urls .I also have another function to preload the images in the background inorder cut down on client -server network traffic.The idea of loading these urls and there corresponding links from an xml File residing on the server was that now I can have a webform in my Admin site which can add new images and links , update & delete existing entries .i guess it's slight different approach for Content management ! I hope this is usefull to someone out there !As it sure took me a while . This is my first Post ever providing a solution to a problem !Happy Programming
Code:
<script type="text/javascript" language="javascript"  >
    // JScript File
var xmlDoc ;
pix = new Array();
links = new Array();
var i = 0;
function importXML()
{    //alert('inside importXML 1');
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = populatePIXFireFox;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) populatePIXIE()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("Top_Animation.xml");
}
function populatePIXFireFox()
{   //alert('inside populatePIX');
var x = xmlDoc.getElementsByTagName('Image');
//alert(x.length.toString());
for (i=0;i<x.length;i++)
{
for (j=0;j<x[i].childNodes.length;j++)
{  
    //alert('x[i].childNodes.length =' + x[i].childNodes.length.toString());
if (x[i].childNodes[j].nodeType != 1) continue;
//alert('j= '+ j.toString() +'i= '+ i.toString());
if (j==1)
{
pix[i]=x[i].childNodes[j].firstChild.nodeValue;
//alert('j= '+ j.toString() +'i= '+ i.toString());
            //alert(pix[i].toString());
}
if (j!=1)
{
    //alert('j= '+ j.toString() +'i= '+ i.toString());
    links[i]=x[i].childNodes[j].firstChild.nodeValue;
            //slideshowlinks(x[i].childNodes[j].firstChild.nodeValue);
      
}

}

}
//alert('these are the pics '+pix.toString());
//alert('these are the links '+links.toString());
MM_preloadImages();
slideshow();
}
function populatePIXIE()
{   //alert('inside populatePIX');
var x = xmlDoc.getElementsByTagName('Image');
//alert(x.length.toString());
for (i=0;i<x.length;i++)
{
for (j=0;j<x[i].childNodes.length;j++)
{  
    //alert('x[i].childNodes.length =' + x[i].childNodes.length.toString());
if (x[i].childNodes[j].nodeType != 1) continue;
//alert('j= '+ j.toString() +'i= '+ i.toString());
if (j==0)
{
pix[i]=x[i].childNodes[j].firstChild.nodeValue;
//alert('j= '+ j.toString() +'i= '+ i.toString());
            //alert(pix[i].toString());
}
if (j==1)
{//alert('j= '+ j.toString() +'i= '+ i.toString());
links[i]=x[i].childNodes[j].firstChild.nodeValue;
        //slideshowlinks(x[i].childNodes[j].firstChild.nodeValue);
      
}

}

}
//alert('these are the pics '+pix.toString());
//alert('these are the links '+links.toString());
MM_preloadImages();
slideshow();
}
function slideshow(){
setInterval("change()", 1000);
}
function change(){
document.images.slide.src = pix[i];
i = i + 1;
if (i > (pix.length-1)) {i = 0}
}


function MM_preloadImages() { //v3.0
    // counter
     var i = 0;
     // create object
     imageObj = new Image();
// set image list
     images =pix
      // start preloading
     for(i=0; i<=pix.length; i++)
     {
          imageObj.src=pix[i];
     }
}
function gotoshow(){
if (!window.winslide||winslide.closed)
winslide=window.open(links[i])
else
winslide.location=links[i]
winslide.focus()
}

</script>
</head>
<body  onload="importXML();">
<a href="javascript:gotoshow();"><img src="[URL unfurl="true"]http://localhost/Clipstone_Yamaha/images/header1.jpg"[/URL]  id="slide"/></a>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top