Jump to content

[SOLVED] 2 onload linked scripts problem


greenday

Recommended Posts

Hi

 

I have 2 linked .js files in my page, each uses the onload to trigger the script. Only one of the scripts will work at a time. Is there anything i can do to get them both to work? I am new to javascript!

 

Here are the links:

 

<script src="scripts/custom-form-elements.js" type="text/javascript"></script>

 

Has this at the very END of the script:

window.onload = Custom.init;

 

And the other linked .js file is:

<script src="scripts/accordian.js" type="text/javascript"></script>

 

Which has this at this at the begining after the variables are set:

 

window.onload = function() {

 

Any help much apreciated  8)

 

 

Link to comment
https://forums.phpfreaks.com/topic/121624-solved-2-onload-linked-scripts-problem/
Share on other sites

thanks both for your help. I am not sure on the syntax to group them together. Here are the two scripts, if anyone could show me how to group them together, it would be a useful lesson. thanks.

 

var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "190";

/* No need to change anything after this */

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; z-index: 5; }</style>');

var Custom = {
init: function() {
	var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
	for(a = 0; a < inputs.length; a++) {
		if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
			span[a] = document.createElement("span");
			span[a].className = inputs[a].type;

			if(inputs[a].checked == true) {
				if(inputs[a].type == "checkbox") {
					position = "0 -" + (checkboxHeight*2) + "px";
					span[a].style.backgroundPosition = position;
				} else {
					position = "0 -" + (radioHeight*2) + "px";
					span[a].style.backgroundPosition = position;
				}
			}
			inputs[a].parentNode.insertBefore(span[a], inputs[a]);
			inputs[a].onchange = Custom.clear;
			span[a].onmousedown = Custom.pushed;
			span[a].onmouseup = Custom.check;
			document.onmouseup = Custom.clear;
		}
	}
	inputs = document.getElementsByTagName("select");
	for(a = 0; a < inputs.length; a++) {
		if(inputs[a].className == "styled") {
			option = inputs[a].getElementsByTagName("option");
			active = option[0].childNodes[0].nodeValue;
			textnode = document.createTextNode(active);
			for(b = 0; b < option.length; b++) {
				if(option[b].selected == true) {
					textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
				}
			}
			span[a] = document.createElement("span");
			span[a].className = "select";
			span[a].id = "select" + inputs[a].name;
			span[a].appendChild(textnode);
			inputs[a].parentNode.insertBefore(span[a], inputs[a]);
			inputs[a].onchange = Custom.choose;
		}
	}
},
pushed: function() {
	element = this.nextSibling;
	if(element.checked == true && element.type == "checkbox") {
		this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
	} else if(element.checked == true && element.type == "radio") {
		this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
	} else if(element.checked != true && element.type == "checkbox") {
		this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
	} else {
		this.style.backgroundPosition = "0 -" + radioHeight + "px";
	}
},
check: function() {
	element = this.nextSibling;
	if(element.checked == true && element.type == "checkbox") {
		this.style.backgroundPosition = "0 0";
		element.checked = false;
	} else {
		if(element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			group = this.nextSibling.name;
			inputs = document.getElementsByTagName("input");
			for(a = 0; a < inputs.length; a++) {
				if(inputs[a].name == group && inputs[a] != this.nextSibling) {
					inputs[a].previousSibling.style.backgroundPosition = "0 0";
				}
			}
		}
		element.checked = true;
	}
},
clear: function() {
	inputs = document.getElementsByTagName("input");
	for(var b = 0; b < inputs.length; b++) {
		if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
			inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
		} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
			inputs[b].previousSibling.style.backgroundPosition = "0 0";
		} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
			inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
		} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
			inputs[b].previousSibling.style.backgroundPosition = "0 0";
		}
	}
},
choose: function() {
	option = this.getElementsByTagName("option");
	for(d = 0; d < option.length; d++) {
		if(option[d].selected == true) {
			document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
		}
	}
}
}
window.onload = Custom.init;

 

var accordion;
var accordionTogglers;
var accordionContents;

window.onload = function() {
accordionTogglers = document.getElementsByClassName('accToggler');

accordionTogglers.each(function(toggler){
	//remember the original color
	toggler.origColor = toggler.getStyle('background-color');
	//set the effect
	toggler.fx = new Fx.Color(toggler, 'background-color');
});

accordionContents = document.getElementsByClassName('accContent');

accordion = new Fx.Accordion(accordionTogglers, accordionContents,{
	//when an element is opened change the background color to blue
	onActive: function(toggler){
		toggler.fx.toColor('#6899CE');
	},
	onBackground: function(toggler){
		//change the background color to the original (green) 
		//color when another toggler is pressed
		toggler.setStyle('background-color', toggler.origColor);
	}		
});
}

Ken2k7, thanks for your help, but you need to be more specific, i mentioned that i am new to javascript. I made the change as suggested and it doesnt work. Is that all i need to do? Should i copy and paste one script into he other aswell? If so which script at the begining? I really need fool proof advice as i am new. Thanks.

Change:

window.onload = Custom.init;

 

To:

window.onload = Custom.init();

 

Not so. If you want to group multiple actions into one onload call like this, you need to add your Custom.init() into the function declaration you mention:

window.onload = function()
{
  Custom.init();
  // ... follow through with the rest of your code
}

Great, thanks so much, got it them to work together now  8)  Here is the final code incase it may help others out:

 

/*

var accordion;
var accordionTogglers;
var accordionContents;

var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "190";

/* No need to change anything after this */

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; z-index: 5; }</style>');

var Custom = {
init: function() {
	var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
	for(a = 0; a < inputs.length; a++) {
		if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
			span[a] = document.createElement("span");
			span[a].className = inputs[a].type;

			if(inputs[a].checked == true) {
				if(inputs[a].type == "checkbox") {
					position = "0 -" + (checkboxHeight*2) + "px";
					span[a].style.backgroundPosition = position;
				} else {
					position = "0 -" + (radioHeight*2) + "px";
					span[a].style.backgroundPosition = position;
				}
			}
			inputs[a].parentNode.insertBefore(span[a], inputs[a]);
			inputs[a].onchange = Custom.clear;
			span[a].onmousedown = Custom.pushed;
			span[a].onmouseup = Custom.check;
			document.onmouseup = Custom.clear;
		}
	}
	inputs = document.getElementsByTagName("select");
	for(a = 0; a < inputs.length; a++) {
		if(inputs[a].className == "styled") {
			option = inputs[a].getElementsByTagName("option");
			active = option[0].childNodes[0].nodeValue;
			textnode = document.createTextNode(active);
			for(b = 0; b < option.length; b++) {
				if(option[b].selected == true) {
					textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
				}
			}
			span[a] = document.createElement("span");
			span[a].className = "select";
			span[a].id = "select" + inputs[a].name;
			span[a].appendChild(textnode);
			inputs[a].parentNode.insertBefore(span[a], inputs[a]);
			inputs[a].onchange = Custom.choose;
		}
	}
},
pushed: function() {
	element = this.nextSibling;
	if(element.checked == true && element.type == "checkbox") {
		this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
	} else if(element.checked == true && element.type == "radio") {
		this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
	} else if(element.checked != true && element.type == "checkbox") {
		this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
	} else {
		this.style.backgroundPosition = "0 -" + radioHeight + "px";
	}
},
check: function() {
	element = this.nextSibling;
	if(element.checked == true && element.type == "checkbox") {
		this.style.backgroundPosition = "0 0";
		element.checked = false;
	} else {
		if(element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			group = this.nextSibling.name;
			inputs = document.getElementsByTagName("input");
			for(a = 0; a < inputs.length; a++) {
				if(inputs[a].name == group && inputs[a] != this.nextSibling) {
					inputs[a].previousSibling.style.backgroundPosition = "0 0";
				}
			}
		}
		element.checked = true;
	}
},
clear: function() {
	inputs = document.getElementsByTagName("input");
	for(var b = 0; b < inputs.length; b++) {
		if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
			inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
		} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
			inputs[b].previousSibling.style.backgroundPosition = "0 0";
		} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
			inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
		} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
			inputs[b].previousSibling.style.backgroundPosition = "0 0";
		}
	}
},
choose: function() {
	option = this.getElementsByTagName("option");
	for(d = 0; d < option.length; d++) {
		if(option[d].selected == true) {
			document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
		}
	}
}
}
window.onload = function()
{
  Custom.init();
{
accordionTogglers = document.getElementsByClassName('accToggler');

accordionTogglers.each(function(toggler){
	//remember the original color
	toggler.origColor = toggler.getStyle('background-color');
	//set the effect
	toggler.fx = new Fx.Color(toggler, 'background-color');
});

accordionContents = document.getElementsByClassName('accContent');

accordion = new Fx.Accordion(accordionTogglers, accordionContents,{
	//when an element is opened change the background color to blue
	onActive: function(toggler){
		toggler.fx.toColor('#6899CE');
	},
	onBackground: function(toggler){
		//change the background color to the original (green) 
		//color when another toggler is pressed
		toggler.setStyle('background-color', toggler.origColor);
	}		
});
}}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.