Call Javascript Function from JQuery
I have Javascript function from google maps tutorial
<script type="text/javascript"> function initialize() { ... } </script And in the textbook, it is called by the onload body. But since I have jquery code, I thought I would just call from there. So I just tried
$(document).ready(function() { initialize(); }); But then no jquery works at all. How to call this function?
This should work, see http://jsfiddle.net/L3U8T/ . You should have some error in your initialization or somewhere else.
Why not try calling a function outside of jquery. Because the function is defined in Raw Javascript.
Are you sure to remove onLoad="initialize()" from the <body> ?
For example, change this:
<body onLoad="initialize()"> For this:
<body> And then name your script as you originally tried:
$(document).ready(function() { initialize(); }); So it will look like this:
<html> <head> <title>My page</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script> $(document).ready(function() { initialize(); }); </script> <script> function initialize() { var latlng = new google.maps.LatLng(40.7562008,-73.9903784); var myOptions = { zoom: 18, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); } </script> </head> <body> <!--Start Google Map--> <div id="map"></div> <!--End Google Map--> </body> </html> All you have to do is call the initialize function as a function for onready, instead of putting it in an anonymous function