Elad Benda
Elad Benda

Reputation: 36664

<body onload=foo()> doesn't call foo

I'm trying to run this code, but initialize() isn't called.

http://jsfiddle.net/r3sA8/

Can someone tell why ?

Upvotes: 1

Views: 345

Answers (3)

chrisn
chrisn

Reputation: 2135

In addition to creating a new script block as others have mentioned, you haven't ended your initialize function.

function initialize() {
            if (GBrowserIsCompatible()) {
                map = new GMap2(document.getElementById("map"));
                map.setCenter(new GLatLng(51.5, -0.1), 10);
                map.setUIToDefault();

                geocoder = new GClientGeocoder();

                showAddressOnLoad("28 Gronemann Street, Tel Aviv, Israel");
            }
.
.
.

You should have another } to end your function. See this updated jsFiddle

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816780

The error is

Uncaught ReferenceError: initialize is not defined.

If a script element has a src element, like in your example, its content is not evaluated by the browser.

The solution is to put the code in its own script tag:

<script src="..."></script>
<script>
    // your code here
</script>

Side note regarding jsFiddle: You should only put content which goes into the body in the HTML pane. And jsFiddle can certainly not evaluate special asp tags.

Upvotes: 2

Rob W
Rob W

Reputation: 349102

Code within a <script> block is not evaluated when the tag has the src attribute set.

Replace the following:

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyCP_o3czlByfGNa-S1YYMBAqfYKNg5nRKU&sensor=false"
    type="text/javascript">
    var map;
    var geocoder;
    ...

with

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyCP_o3czlByfGNa-S1YYMBAqfYKNg5nRKU&sensor=false"></script>
<script>
    var map;
    var geocoder;
    ...

(The <script src="..."> tag is closed, and a new script block is started using <script>).

Upvotes: 7

Related Questions