Solved

javascript onload

Posted on 2011-09-02
3
1,007 Views
Last Modified: 2012-08-13
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAJxYcItOgkqR8R_oLqIOOTBQacz0bPLQUXEQFHZqrZvMEpHZ5XRSCJ0pmFLc3gNPINynksheGWBUQdg" 
type="text/javascript"></script>
<script type="text/javascript">
function initialize(from, to) {
	if (GBrowserIsCompatible()) {    
	  	map = new GMap2(document.getElementById("map_canvas"));
	  	gdir = new GDirections(map, document.getElementById("directions"));
	  	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	  	GEvent.addListener(gdir, "error", handleErrors);
	  	setDirections(from, to, "en_US");
	}
} 
</script>
</head>

<body onload="initialize('fromaddress','toaddress')" onunload="GUnload()" >
<div id="map_canvas" style="width: 500px; height: 400px"></div>

<input type="hidden" name="fromaddress" value="100 Main Street. Los Angeles, CA 90032" />
<input type="hidden" name="toaddress" value="200 Main Street. Los Angeles, CA 90032" />



</body>
</html>

Open in new window


I have attached codes in html/javascript.
How can I load both hidden value to Onload event in Javascript.
Assume  both hidden fields already have value there .

Thank
0
Comment
Question by:Webboy2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Expert Comment

by:Gregg
ID: 36476514
Are you trying to set the value of the hidden form fields? And run the initialize function during the window onload event? If so please see attached sample.

You will need to uncomment the initialize function and delete the alert. Hope this helps.

Javascript file

//**************************************
//Window Load 
//**************************************

/* put all onload functions and code here.*/
addEvent(window, "load", function() {
	//Set To Address
	var from = document.getElementById("fromaddress");
	from.value="100 Main Street. Los Angeles, CA 90032";
	
	//Set From Address
	var to = document.getElementById("toaddress")
	to.value="200 Main Street. Los Angeles, CA 90032";
	
	//initialize method call.
	//initialize(from,to);
	
	alert(to.value);
});

//**************************************
//Window Load 
//**************************************
function initialize(from, to) {
	if (GBrowserIsCompatible()) {    
	  	map = new GMap2(document.getElementById("map_canvas"));
	  	gdir = new GDirections(map, document.getElementById("directions"));
	  	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	  	GEvent.addListener(gdir, "error", handleErrors);
	  	setDirections(from, to, "en_US");
	}
} 


//********************************
//Event Handlers
//********************************
// bind the event handlers
function addEvent(elem, evtType, func) {
	if (elem.addEventListener) {
		elem.addEventListener(evtType, func, false);
	}
	else if (elem.attachEvent) {
		elem.attachEvent("on" + evtType, func);
	}
	else {
		elem["on" + evtType] = func;
	}
}

Open in new window

Html file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAJxYcItOgkqR8R_oLqIOOTBQacz0bPLQUXEQFHZqrZvMEpHZ5XRSCJ0pmFLc3gNPINynksheGWBUQdg" type="text/javascript"></script>
<script src="test.js" type="text/javascript"></script>

</head>

<body>
<div id="map_canvas" style="width: 500px; height: 400px"></div>

<input type="hidden" name="fromaddress" value="" id="fromaddress"/>
<input type="hidden" name="toaddress" value="" id="toaddress" />


</body>
</html>

Open in new window

test.html
test.js
0
 
LVL 10

Expert Comment

by:Gregg
ID: 36476515
Are you trying to set the value of the hidden form fields? And run the initialize function during the window onload event? If so please see attached sample.

You will need to uncomment the initialize function and delete the alert. Hope this helps.

Javascript file

//**************************************
//Window Load 
//**************************************

/* put all onload functions and code here.*/
addEvent(window, "load", function() {
	//Set To Address
	var from = document.getElementById("fromaddress");
	from.value="100 Main Street. Los Angeles, CA 90032";
	
	//Set From Address
	var to = document.getElementById("toaddress")
	to.value="200 Main Street. Los Angeles, CA 90032";
	
	//initialize method call.
	//initialize(from,to);
	
	alert(to.value);
});

//**************************************
//Window Load 
//**************************************
function initialize(from, to) {
	if (GBrowserIsCompatible()) {    
	  	map = new GMap2(document.getElementById("map_canvas"));
	  	gdir = new GDirections(map, document.getElementById("directions"));
	  	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	  	GEvent.addListener(gdir, "error", handleErrors);
	  	setDirections(from, to, "en_US");
	}
} 


//********************************
//Event Handlers
//********************************
// bind the event handlers
function addEvent(elem, evtType, func) {
	if (elem.addEventListener) {
		elem.addEventListener(evtType, func, false);
	}
	else if (elem.attachEvent) {
		elem.attachEvent("on" + evtType, func);
	}
	else {
		elem["on" + evtType] = func;
	}
}

Open in new window

Html file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAJxYcItOgkqR8R_oLqIOOTBQacz0bPLQUXEQFHZqrZvMEpHZ5XRSCJ0pmFLc3gNPINynksheGWBUQdg" type="text/javascript"></script>
<script src="test.js" type="text/javascript"></script>

</head>

<body>
<div id="map_canvas" style="width: 500px; height: 400px"></div>

<input type="hidden" name="fromaddress" value="" id="fromaddress"/>
<input type="hidden" name="toaddress" value="" id="toaddress" />


</body>
</html>

Open in new window

0
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 500 total points
ID: 36476983
You should be focusing on writing unobtrusive javascript (i.e. separating your javascript from your html).
To do that requires a simple change, which is using the window.onload event instead of <body onload="">

Applying this to your page should give you something that looks like this:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAJxYcItOgkqR8R_oLqIOOTBQacz0bPLQUXEQFHZqrZvMEpHZ5XRSCJ0pmFLc3gNPINynksheGWBUQdg" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function() {
    var from = document.getElementByName('fromaddress').value;
    var to = document.getElementByName('toaddress').value;
    initialize(from, to);
}

window.onunload = function() {
    GUnload();
}

function initialize(from, to) {
	if (GBrowserIsCompatible()) {    
	  	map = new GMap2(document.getElementById("map_canvas"));
	  	gdir = new GDirections(map, document.getElementById("directions"));
	  	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	  	GEvent.addListener(gdir, "error", handleErrors);
	  	setDirections(from, to, "en_US");
	}
} 
</script>
</head>

<body>
    <div id="map_canvas" style="width: 500px; height: 400px"></div>
    <input type="hidden" name="fromaddress" value="100 Main Street. Los Angeles, CA 90032" />
    <input type="hidden" name="toaddress" value="200 Main Street. Los Angeles, CA 90032" />
</body>
</html>

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
EF5: Update Model from Database not working 3 83
How to open a new browser tab after executing php script 20 49
Pass a js value to an html form 5 43
Jquery syntax 12 30
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question