Forum Moderators: open
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type="text/javascript">
var a = function() {
alert("I'm executing!");
};
a();
</script>
</head>
<body>
...
Yes, just put the code in it's own <script> tag inside the <head> tag before your CSS includes, and non-related javascript file includes.
Yes, I presumed you wanted something running absolutely instantly - hence my instruction to include it in the head tag (As Fotiman says - this is typically bad practice, but if you need something running pre-dom load it's the only solution as far as I know).
getElementById should function as you want it to if you move the script to the end of your body tag, as the elements will exist before the code is executed.
function choiseonload(){
document.choise.a[0].checked=true;
document.getElementById("uno").style.display = "block";
document.getElementById("dos").style.display = "none";
document.getElementById("tres").style.display = "none";
}
var alreadeexecuted=0 //indicates if already executed
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", function(){alreadeexecuted=1; choiseonload()}, false)
else if (document.all && !window.opera){
document.write('<script type="text/javascript" id="choiseid" defer="defer" src="javascript:void(0)"><\/script>')
var choiseid=document.getElementById("choiseid")
choiseid.onreadystatechange=function(){
if (this.readyState=="complete"){
alreadeexecuted=1
choiseonload()
}
}
}
window.onload=function(){
setTimeout("if (!alreadeexecuted) choiseonload()", 0)
}
[edited by: phranque at 2:28 am (utc) on Dec 1, 2013]
[edit reason] disabled graphic smileys [/edit]
The easiest way to accomplish what you are trying to do is just to put your scripts before the closing </body> tag.
Thats where body ends.
I want to do this:
document.choise.a[1].checked=true;
document.getElementById("uno").style.display = "block";
document.getElementById("dos").style.display = "none";
document.getElementById("tres").style.display = "none";
Yes I know, but I dont understand how the quickest way can be to add script at end of document
I want to do this:
If you want a specific checkbox to be checked by default, then make it checked in the markup with the checked attribute.
Again, set the default styles in css rules.
You can't call document.getElementById with an ID for an element that hasn't loaded in the DOM yet. So you can either call it when the DOM is ready, or slightly later when the window load event has fired. If you put your script at the end of your document just before the closing </body> tag, then that's technically just before any DOM events will fire that indicate the DOM is ready.
<script type="text/javascript">
function choiseonload(){
document.choise.a[0].checked=true;
document.getElementById("uno").style.display = "block";
document.getElementById("dos").style.display = "none";
document.getElementById("tres").style.display = "none";
}
var alreadeexecuted=0 //indicates if already executed
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", function(){alreadeexecuted=1; choiseonload()}, false)
else if (document.all && !window.opera){
document.write('<script type="text/javascript" id="choiseid" defer="defer" src="javascript:void(0)"><\/script>')
var choiseid=document.getElementById("choiseid")
choiseid.onreadystatechange=function(){
if (this.readyState=="complete"){
alreadeexecuted=1
choiseonload()
}
}
}
window.onload=function(){
setTimeout("if (!alreadeexecuted) choiseonload()", 0)
}
function toggle(elemento) {
if(elemento.value=="1") {
document.getElementById("uno").style.display = "block";
document.getElementById("dos").style.display = "none";
document.getElementById("tres").style.display = "none";
}
else{
if(elemento.value=="2"){
document.getElementById("uno").style.display = "none";
document.getElementById("dos").style.display = "block";
document.getElementById("tres").style.display = "block";
}
}
}
function OnSubmitForm()
{
if(document.choise.a[0].checked == true)
{
if ( ( document.searcherform.s[0].checked == false ) && ( document.searcherform.s[1].checked == false ) )
{ alert ( "Var god och välj Alla datum eller Valda datum" );
return false
}
document.searcherform.action="http://www.example.com/svenska/sokare.php?z=z&d=d&t=t&s=s&day=day&month=month&day2=day2&month2=month2&e=e"
}
else
if(document.choise.a[1].checked == true)
{
document.searcherformsales.action="http://www.example.com/svenska/search.php?z=z&t=t&b=b&p=p&p1=p1&enviar=enviar"
document.searcherformsales2.action="http://www.example.com/svenska/search.php?id=id&enviarid=enviarid"
}
return true;
}
</script>