Click to See Complete Forum and Search --> : Another scripting problem


pgo
September 15th, 2003, 09:26 AM
I just got my first problem answered so I thought I'd pose another one.

The following function is called from a main page and displays a picture with some info. I want to have next and previous buttons to show other images i the list. To do this I tried calling the function again when the the 'next image' is pressed but it generates an error. I cannot figure out why.

A seperate problem is that when I try to increment the variable num it treats num like a string and simply concatenates a 1 to the previous number.

Note that the variables n1, h1, w1, and d1 are global variables (arrays).

I would really appreciate any help anyone could give.

Per-Gunnar

-----------

function image_win(gall, num)
{
var img;
var len;
var desc = '';
var next = num + 1;

img = 'images/' +n1[num]+ '.jpg';
gallname = 'graphics/' +gall+ '_name.gif';

len = d1[num].length;
for (a=0;a<len;a++) {
if (d1[num].charAt(a) == '_') desc = desc + ' ';
else desc = desc + d1[num].charAt(a);
}

document.writeln("<HTML>\n<HEAD>\n\t<TITLE>pgo images - if window blank press F5</TITLE>\n");

document.writeln("</HEAD>\n\n<BODY BGCOLOR='#323232' TEXT='#FFFFFF'>");
document.writeln("\t<TABLE BORDER='0' VALIGN='center' WIDTH='100%' HEIGHT='100%'>");

document.writeln("\t\t<TD ALIGN = 'left'>\n\t\t\t<IMG SRC=" +gallname+ " width='250' HEIGHT='26' ALT=''>\n\t\t</TD>");

document.writeln("\t\t<TD ALIGN = 'center' COLSPAN = 2>");


document.writeln("<A HREF=javascript:image_win('" +gall+ "','"+next+"')>next image</A>");


document.writeln("\t\t\t<FONT SIZE='2' FACE='verdana'>" +n1[num]+ " - " +desc);

document.writeln("\t\t\t<BR><BR><CENTER>\n\t\t\t<IMG SRC=" +img+ " NAME='pic' WIDTH=" +w1[num]+ " HEIGHT=" +h1[num]+ " BORDER='30' ALT=''>");
document.writeln("\t\t</TD>\n\t</TABLE>");
document.writeln("</BODY>\n</HTML>")
}


The above code generates the following web page for example.

<HTML>
<HEAD>
<TITLE>pgo images - if window blank press F5</TITLE>

</HEAD>

<BODY BGCOLOR='#323232' TEXT='#FFFFFF'>
<TABLE BORDER='0' VALIGN='center' WIDTH='100%' HEIGHT='100%'>
<TD ALIGN = 'left'>
<IMG SRC=graphics/adin_name.gif width='250' HEIGHT='26' ALT=''>
</TD>
<TD ALIGN = 'center' COLSPAN = 2>
<A HREF=javascript:image_win('adin','5')>next image</A>
<FONT SIZE='2' FACE='verdana'>030830-012 - my test
<BR><BR><CENTER>
<IMG SRC=images/030830-012.jpg NAME='pic' WIDTH=399 HEIGHT=600 BORDER='30' ALT=''>
</TD>
</TABLE>
</BODY>
</HTML>

n_p_k76
September 16th, 2003, 12:12 AM
Try replacing

<A HREF=java script:image_win('adin','5')>next image</A>

and

document.writeln("<A HREF=java script:image_win('" +gall+ "','"+next+"')>next image</A>");

WITH

<A HREF="javascript:image_win('adin','5')">next image</A>

document.writeln("<A HREF=\"javascript:image_win('" +gall+ "','"+next+"')\">next image</A>");

There's not supposed to any space between 'java' and 'script'
I can't think of anything else, without the global arrays that you mentioned, I can't 'run' the stuff here.

A seperate problem is that when I try to increment the variable num it treats num like a string and simply concatenates a 1 to the previous number.

Javascript does runtime typecasting of variables , so ints change to strings and so on depending.
You could work around this by doing something like this :

var a = 10;
//variable a behaves like an integer at this point
a = 10 + '';
//String concatenation changes variable a to behave like a
//String from this point on.So (a + 1) will result in '101'.
a = (a*1) + 1;
// Multiplying by 1 will change it back to behave like an int, and
//the result will be 11.

There may be other more legant ways of doing it.

------------------------------------------------------------------
Prem

pgo
September 16th, 2003, 06:43 AM
many many thanks for your reply.

I will give this a try over the next few days.

Per-Gunnar

pgo
September 20th, 2003, 08:22 AM
Unfortunately, the last idea tdidn't work.

However, the fault is on my side, as I'm clearly not understanding global values very well. When I click the next button it doesn't know the "global " variable n1, h1,w1, d1, etc.

I tried adding a reference to the function (image_prep(gall);) that defines these variables in the script itself but it throws up an error. The code now looks like:

function image_win(gall, num)
{
var img;
var len;
var desc = '';
var next = (num*1) +1;

image_prep(gall);

img = 'images/' +n1[num]+ '.jpg';
gallname = 'graphics/' +gall+ '_name.gif';

len = d1[num].length;
for (a=0;a<len;a++) {
if (d1[num].charAt(a) == '_') desc = desc + ' ';
else desc = desc + d1[num].charAt(a);
}

document.writeln("<HTML>\n<HEAD>\n\t<TITLE>pgo images - if window blank press F5</TITLE>\n");


document.writeln("<SCRIPT LANGUAGE='JavaScript' src='image_win.js'></SCRIPT>");


document.writeln("</HEAD>\n\n<BODY BGCOLOR='#323232' TEXT='#FFFFFF' ALINK='#FFFFFF' VLINK='#FFFFFF' LINK='#FFFFFF'>");
document.writeln("\t<TABLE BORDER='1' VALIGN='center' WIDTH='100%' HEIGHT='100%'>");

document.writeln("\t\t<TD ALIGN = 'center'>\n\t\t\t<IMG SRC=\'" +gallname+ "\' width='250' HEIGHT='26' ALT=''>");

document.writeln("\t\t\t<BR><BR><BR><BR><BR><BR><B><FONT SIZE='2' FACE='verdana'>" +desc+ "\n\t\t</TD>");

document.writeln("\t\t<TD ALIGN = 'center'>");
document.writeln("\t\t\t<A HREF=javascript:image_win('" +gall+ "','"+next+"')>next image</A>");


document.writeln("\t\t\t<BR><BR><CENTER>\n\t\t\t<IMG SRC=\'" +img+ "\' NAME='pic' WIDTH=\'" +w1[num]+ "\' HEIGHT=\'" +h1[num]+ "\' BORDER='30' ALT=" +n1[num]+ ">");
document.writeln("\t\t</TD>\n\t</TABLE>");
document.writeln("</BODY>\n</HTML>")
}


Here is part of the function image_prep (I've not included it all as the file is very long)

function image_prep(gal)
{
version="0";
browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
if (browserName == "Netscape" && browserVer >= 3)
version="n3";
if (browserName == "Microsoft Internet Explorer" && browserVer >= 3)
version="n3";
if (version == "n3")
{
if (gal=="australia") prep_australia();
else if (gal=="botswana") prep_botswana();

}
}

function prep_australia()
{
total = 3;
gall = "australia";
n1 = new Array(total);
w1 = new Array(total);
h1 = new Array(total);
d1 = new Array(total);

n1[0] = "976402"; w1[0] = 388; h1[0] = 600; d1[0] = "royal_national_park,_sydney,_australia";
n1[1] = "976405"; w1[1] = 394; h1[1] = 600; d1[1] = "royal_national_park,_sydney,_australia";
n1[2] = "976407"; w1[2] = 600; h1[2] = 389; d1[2] = "opera_house,_sydney,_australia";
}

Again, any hep would be immensely appreciated.

Per-Gunnar

pgo
September 20th, 2003, 09:33 AM
Please ignore my last post.

I have figured it out!

Thanks for looking anyway.

Per-Gunnar