1

I'm a javascript new comer so I apologize in advance if this is a "newb" question.

I'm making a web app that displays a google maps, map with a marker in your location. And then later more markers can be added to it using the add_marker function. A stripped down version of the code is:

<!DOCTYPE html> 
<html> 
    <head> 
        <link rel="stylesheet" type="text/css" href="./common.css"/> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <script type="text/javascript" src=".\common.js"></script> 
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
        function add_stops(lat,lng)
        {
            var loc = new google.maps.LatLng(lat,lng);
            var marker = new google.maps.Marker
            ({
                position: loc,
                map: this.map,
                marker: marker
            });
        }
        </script>
        <title>Local Stops</title> 
    </head> 
    <body> 
        <div id="map_canvas"></div>
      <script type="text/javascript">initialize(null,null,16)</script> 
      <input type="button" id="10665"  value="Stop 10665" onmouseover="add_stops(49.89995, -97.14124, 10665)"/> 
      <script type="text/javascript">add_stops(49.89995, -97.14124)</script>
    </body> 
</html>

My problem is the add_stops function wont run when called in the script tags in the body but it works fine when called by the onmouseover event for the button.

In short what would cause a function to run from an event but not when called from the body, and what can I do to fix it.

Thanks, any input is greatly appreciated.

**edited for spelling

Jonas G. Drange
  • 8,749
  • 2
  • 27
  • 38
curriegrr
  • 566
  • 5
  • 19

3 Answers3

1

Some external JavaScript functions are not yet fired when the page is loaded. Try using setTimeout to delay the execution, e.g. fire your function after 5 seconds (5000) after page load, to give the chance for the Google's script to execute itself by that time.

EDIT: code snippet. Something like:

window.setTimeout(function() {add_stops(49.89995, -97.14124)}, 5000);
jakub.g
  • 38,512
  • 12
  • 92
  • 130
  • function still didn't fire after delay. – curriegrr Sep 13 '11 at 19:05
  • 1
    Have you wrapped your code in `function() {...code here...}`? – jakub.g Sep 13 '11 at 19:10
  • retried this it works now thank you so much spent an hour banging my head against the wall. – curriegrr Sep 13 '11 at 19:14
  • You're welcome. Had similar problems recently. An off-topic piece of advice in case you ever needed to use window.setTimeout in Greasemonkey userscript: [do not pass the code as a string](http://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greasemonkey-pitfalls.html?page=3) - works in normal JS, but not in Greasemonkey. – jakub.g Sep 13 '11 at 19:27
0
  <script type="text/javascript">add_stops(49.89995, -97.14124, 10665)</script>

This call has 3 parameters, your method definition has only two. Probably it has to do with that.

  • sorry stripped down the function to try and make the question more clear. Have corrected that, question still stands. – curriegrr Sep 13 '11 at 18:47
0

I guess you could use the "onload" event of the body tag to call this function instead of writing a script for calling it.

Praveen
  • 1,449
  • 16
  • 25