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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Someone SIMPLIFY this code? 1

Status
Not open for further replies.

23kStudios

IS-IT--Management
Feb 9, 2005
13
US
Is there any way to simplify this incredibly???

function goForward() {
var page = getpagenumber(); //gets the URL of image frame
var otherpage = parent.image.location.href;

//checks through all possible URLs of Image frame and then puts to next one.
if (page == "01.html") {
parent.image.location.href = '02.html';
MM_swapImage('icon2','','images/ic_concord2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}

if (page == "02.html") {
parent.image.location.href = '03.html';
MM_swapImage('icon3','','images/ic_sapgolf2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}

if (page == "03.html") {
parent.image.location.href = '04.html';
MM_swapImage('icon4','','images/ic_wiredbox2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}

if (page == "04.html") {
parent.image.location.href = '05.html';
MM_swapImage('icon5','','images/ic_toptier2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}

if (page == "05.html") {
parent.image.location.href = '06.html';
MM_swapImage('icon6','','images/ic_sapbox2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}

if (page == "06.html") {
parent.image.location.href = '07.html';
MM_swapImage('icon7','','images/ic_sapgrandprix2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}

if (page == "07.html") {
parent.image.location.href = '08.html';
}

if (page == "08.html") {
parent.image.location.href = '09.html';
MM_swapImage('icon9','','images/ic_sapcardboard2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}

if (page == "09.html") {
parent.image.location.href = '10.html';
MM_swapImage('icon10','','images/ic_sapgame2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}

if (page == "10.html") {
parent.image.location.href = '11.html';
}

if (page == "11.html") {
parent.image.location.href = '12.html';
MM_swapImage('icon12','','images/ic_towersbro2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}

if (page == "12.html") {
parent.image.location.href = '13.html';
MM_swapImage('icon13','','images/ic_sappaintcan2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
}

if (page == "13.html") {
parent.image.location.href = '14.html';
}

if (page == "14.html") {
parent.image.location.href = '15.html';
}

if (page == "15.html") {
parent.image.location.href = '01.html';
MM_swapImage('icon1','','images/ic_fortune2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
// * 8 missing
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
// * 11 missing
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
}
}
 
that was fun...this should get you started:
Code:
function goForward() {
	var imgs = new Array(
		"images/ic_fortune1.jpg",
		"images/ic_concord1.jpg",
		"images/ic_sapgolf1.jpg",
		"images/ic_wiredbox1.jpg",
		"images/ic_toptier1.jpg",
		"images/ic_sapbox1.jpg",
		"images/ic_sapgrandprix1.jpg",
		null,
		"images/ic_sapcardboard1.jpg",
		"images/ic_sapgame1.jpg",
		null,
		"images/ic_towersbro1.jpg",
		"images/ic_sappaintcan1.jpg",
		null,
		null
	);
	
	var page = getpagenumber(); //gets the URL of image frame
	var pagenum = parseInt(page.replace(".html", ""), 10);
	
	var nextpagenum = ((pagenum+1) > 15) ? 1 : (pagenum+1);	
	var nextpage = lz(nextpagenum) + ".html";
	
	var otherpage = parent.image.location.href;

	parent.image.location.href = nextpage;

	/*
	 * for each image, we want ---2.jpg for nextpagenum image
	 * and ---1.jpg for all others
	 */
	for (var x = 0; x < imgs.length; x++) {
		var num = x + 1;		
		
		if (num == nextpagenum) {
			if (imgs[nextpagenum - 1] != null) {
				MM_swapImage("icon" + num, "", imgs[nextpagenum - 1].replace("1.jpg","2.jpg"), 1);
			}
		}
		else {
			if (imgs[x] != null) {
				MM_swapImage("icon" + num, "", imgs[x], 1);
			}
		}
	}
}

/**
 * adds a leading zero if n < 10
 */
function lz(n) {
	return (n.length > 1) ? n : "0" + n;
}

i'll send you the bill later. ;-)

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Wow jeff, thanks so much man i will definitely have to give that a try!!!!! Whats your house address i'm sending a stripper over immidiately, haha your amazing ;)

I checked it and it seems to be workin!!!!!!!

Does it go back to 1 when you hit 15?
 
Do you know what the difference would be in code so i can just change it around for a Back button with the exact same format as this?

So if your on page 3, it changes to 2 - and image 2 highlights (2.jpg) and everything else is 1.jpg (unhighlighted).

var nextpagenum = ((pagenum+1) > 15) ? 1 : (pagenum+1);
Is this where if it's 15 it wraps back to 1? Or does it just stop at 15, which i guess would be fine too.

In backwards it would go from 1 -> 15.
I'll keep working at the code you gave me to see if i can figure it out myself, see what i can do. I'm okay with javascript, but nothing special though. More of just general CSS, HTML, C++ kind of guy.

Once again man, Kudos to you !!
 
>> "Does it go back to 1 when you hit 15?"
yes

try this for forward and back:

Code:
function GO(dir) {
	var imgs = new Array(
		"images/ic_fortune1.jpg",
		"images/ic_concord1.jpg",
		"images/ic_sapgolf1.jpg",
		"images/ic_wiredbox1.jpg",
		"images/ic_toptier1.jpg",
		"images/ic_sapbox1.jpg",
		"images/ic_sapgrandprix1.jpg",
		null,
		"images/ic_sapcardboard1.jpg",
		"images/ic_sapgame1.jpg",
		null,
		"images/ic_towersbro1.jpg",
		"images/ic_sappaintcan1.jpg",
		null,
		null
	);
	
	var page = getpagenumber(); //gets the URL of image frame
	var pagenum = parseInt(page.replace(".html", ""), 10);
	
	var nextpagenum = pagenum + dir;
	
	if (nextpagenum > 15) nextpagenum = 1;
	if (nextpagenum < 1) nextpagenum = 15;
	
	var nextpage = lz(nextpagenum) + ".html";
	
	var otherpage = parent.image.location.href;

	parent.image.location.href = nextpage;

	/*
	 * for each image, we want ---2.jpg for nextpagenum image
	 * and ---1.jpg for all others
	 */
	for (var x = 0; x < imgs.length; x++) {
		var num = x + 1;		
		
		if (num == nextpagenum) {
			if (imgs[nextpagenum - 1] != null) {
				MM_swapImage("icon" + num, "", imgs[nextpagenum - 1].replace("1.jpg","2.jpg"), 1);
			}
		}
		else {
			if (imgs[x] != null) {
				MM_swapImage("icon" + num, "", imgs[x], 1);
			}
		}
	}
}

usage:
<input type="button" value="back" onclick="GO(-1)" />
<input type="button" value="forward" onclick="GO(1)" />

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Jeff dude your the man =)
And i filled in the spaces for the null's now.

The only problem i saw with the previous Forward button is that it always added a 0 in front to everything but i took care of that with some simple if's for checking if it's >= 10 or lower than 10.

Going to give this a shot as well, your a codesaver man. Never realized how parelleled Javascript and C++ are! Damn, i took a whole bunch of C++ back in the end, this reminds me of it so much.

One little thing i have that i'm assuming would be easily taken care of with a parameter, is all my mouseOut1() to mouseOut15() commands...

They look like this.

function mouseOut1()
{
var page = getpagenumber(); //gets the URL of image frame
var otherpage = parent.image.location.href;

if (page == "01.html") {
//this keeps the icon to the HOVER state
MM_swapImage('icon1','','images/ic_fortune2.jpg',1);

//this makes sure everything else is in the normal state
MM_swapImage('icon2','','images/ic_concord1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
MM_swapImage('icon8','','images/ic_wiredads1.jpg',1);
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
MM_swapImage('icon11','','images/ic_thinksmall1.jpg',1);
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
// * 14 missing (coming today/tomorrow)
// * 15 missing (adding today/tomorrow)
}

else
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
} // end mouseOut()

function mouseOut2()
{
var page = getpagenumber(); //gets the URL of image frame
var otherpage = parent.image.location.href;

if (page == "02.html") {
//this keeps the icon to the HOVER state
MM_swapImage('icon2','','images/ic_concord2.jpg',1);


//this makes sure everything else is in the normal state
MM_swapImage('icon1','','images/ic_fortune1.jpg',1);
MM_swapImage('icon3','','images/ic_sapgolf1.jpg',1);
MM_swapImage('icon4','','images/ic_wiredbox1.jpg',1);
MM_swapImage('icon5','','images/ic_toptier1.jpg',1);
MM_swapImage('icon6','','images/ic_sapbox1.jpg',1);
MM_swapImage('icon7','','images/ic_sapgrandprix1.jpg',1);
MM_swapImage('icon8','','images/ic_wiredads1.jpg',1);
MM_swapImage('icon9','','images/ic_sapcardboard1.jpg',1);
MM_swapImage('icon10','','images/ic_sapgame1.jpg',1);
MM_swapImage('icon11','','images/ic_thinksmall1.jpg',1);
MM_swapImage('icon12','','images/ic_towersbro1.jpg',1);
MM_swapImage('icon13','','images/ic_sappaintcan1.jpg',1);
// * 14 missing (coming today/tomorrow)
// * 15 missing (adding today/tomorrow)
}

So i've tried to play around with one for loop you had to do all of this but i can't seem to get it to work or encorporate everything at once though.

I know it needs to be something like calling the function in the HTML like mouseOut(1) or mouseOut(13) which then takes that number and makes it's image -2.jpg while the rest -1.jpg.

I won't give up though might have to just pick it back up next week got others to work on too...

appreciate any help though man!!!!!!
your #1 in my book =)
 
Try this as a generic mouseout function to call from every page, and all you have to do is add the page image names in the array at the beginning of the function:
Code:
function mouseOut() 
{
var page = getpagenumber(); //gets the URL of image frame
var otherpage = parent.image.location.href;
var imgs=['fortune', 'concord', 'sapgolf', 'wiredbox', 'toptier', 'sapbox', 'sapgrandprix', 'wiredads', 'sapcardboard', 'sapgame', 'thinksmall', 'towersbro', 'sappaintcan'];

var pagenum = page.substr(0, page.indexOf('.')) * 1;

MM_swapImage('icon' + pagenum,'','images/ic_' + imgs[pagenum - 1) + '2.jpg',1);

for (var ii=0;ii<imgs.length;ii++)
  {
  if ((ii + 1) != pagenum)
    {
    MM_swapImage('icon' + (ii + 1),'','images/ic_' + imgs[ii] + '1.jpg',1);
    }
  }
}
Lee
 
give this a shot:

move the "imgs" array OUTSIDE of the GO() function to make it globally accessible

then add this:
Code:
/**
 * when an icon mouseout happens
 */
function mouseOut(img, page) {	
	for (var x = 0; x < imgs.length; x++) {
		//  skip null entries
		if (imgs[x] == null) continue;
		
		//  keep current page icon in HOVER state
		if (page == (x + 1)) {
			MM_swapImage("icon" + (x+1), "", imgs[x].replace("1.jpg","2.jpg"), 1);
		}
		//  all others in normal state
		else {
			MM_swapImage("icon" + (x+1), "", imgs[x], 1);
		}
	}
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top