/    Sign up×
Community /Pin to ProfileBookmark

Re-using a script

Hi, I’m not the world’s best javascript coder, so I was hoping someone could point me in the right direction. I’m using a script to allow image switching on a page. It works fine when I need to switch one set of images…however, on some pages, I need to “re-use” the script for a second set of image changes. The script allows for one set of images. Here’s what I did…I copied the script and inserted it again in the head. Then, on the copied script, I changed the images and renamed all the variables. I figured this should have worked, but it didn’t, and none of the images would switch. Here’s the script…if anyone can help, I’d greatly appreciate it!
<head>

<script type=”text/javascript”>
<!–

var pic_width=322;
var pic_height=241;

var button_text=”Next Image”;

//pic1.src=”imgs/1896_GWCarver.jpg”;imgs/1896_JimCrow.jpg
if (document.images)
{
pic1= new Image(pic_width,pic_height);
pic1.src=”images/bht_wheatley.jpg”;

pic2= new Image(pic_width,pic_height);
pic2.src=”images/bht_turner.jpg”;

}

var pics= new Array(2)
pics[0]=pic1.src;
pics[1]=pic2.src;

var numpics=2;
var thenum=0;
imgName=”img1″;

function change_it2()
{
if (document.images)
{
document.write(“<IMG SRC='”+pics[thenum]+”‘ border=’0’ width='”+pic_width+”‘ height='”+pic_height+”‘ name=’img1’>n”);

}
}

function change_it3()
{
var x=0;
thenum+=1;

if (thenum>numpics-1)
{
document[imgName].src=pics[0];
thenum=0;
}
else
{
document[imgName].src=pics[thenum];
x+=1;
}

}
//–>
</script>
<body><!–here’s the function call–how do I re-use this script?–>

<script type=”text/javascript”>
<!–
change_it2();
//–>
</script>

<a class=”nextimage” href=”.” onClick=”change_it3();return false;” >next image></a>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@johnnyblotterauthorJan 22.2008 — So here, I just added an "s" to the variable names, and changed the images, in hopes that this would work for me to re-use the script....so now there are two scripts in the head...I'm trying to call this one in a different place,using function change_it4 and function change_it5 with no luck...

<script type="text/javascript">

<!--

var pic_widths=322;

var pic_heights=241;


var button_texts="Next Image";


if (document.images)

{

pic1s= new Image(pic_widths,pic_heights);

pic1s.src="images/bht_usbans.jpg";


pic2s= new Image(pic_widths,pic_heights);

pic2s.src="images/bht_amistad.jpg";

}

var picss= new Array(2)

picss[0]=pic1s.src;

picss[1]=pic2s.src;



var numpicss=2;

var thenums=0;

imgName="img1";

function change_it4()

{

if (document.images)

{

document.write("<IMG SRC='"+picss[thenums]+"' border='0' width='"+pic_widths+"' height='"+pic_heights+"'

name='img1'>n");

}

}

function change_it5()

{

var x=0;

thenums+=1;

if (thenums>numpicss-1)
{
document[imgName].src=picss[0];
thenums=0;
}
else
{
document[imgName].src=picss[thenums];
x+=1;
}

}

//-->

</script>
×

Success!

Help @johnnyblotter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...