1

I have the following code:

function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(53.743317, -0.331004),
    zoom: 12
  };
  var map = new google.maps.Map(document.getElementById("map-canvas"),  mapOptions);
}

function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'https://maps.googleapis.com/maps/api/js?key={APIKEY}&sensor=false&callback=initialize';
  document.body.appendChild(script);
}

If I put

$( window ).load(function() {
    loadScript;
});

It won't load my map. Error in google maps js is Uncaught TypeError: Object #<Object> has no method 'Load'. However if I use

window.onload = loadScript;

It will load it in fine. I have absolutely no idea why.

$(window).load(loadScript());

Also works, just having it as a function that calls it doesn't. Could you tell me the reason of this behavior?

5
  • NB: $( window ).load(function() { loadScript(); }); doesn't work either Commented Mar 5, 2014 at 12:29
  • On Jsfiddle your code seems work in the same way: jQuery: link - window.onload link Commented Mar 5, 2014 at 12:59
  • On jsfiddle it won't work because my API key won't let it so I don't know how you can say they work the same way $(window).load(loadScript()); Works, but putting it in a function doesn't. I really don't understand this Commented Mar 5, 2014 at 13:17
  • Sorry for the misunderstanding, what I wanted to say is that both code snippents on jsFiddle "do not work" in the same way... :-) However I confirm that the same code snippets, on my local pc, with my onw Google API key work without any problem (I have 2 maps of "Hull" in front of me in this moment...) Commented Mar 5, 2014 at 13:32
  • That's odd, maybe it's a rails jquery thing then rather than a normal jquery thing. I'm interested as to why it's different more than anything. Thanks for your help Commented Mar 5, 2014 at 13:47

3 Answers 3

2

You haven't actually invoked loadScript in the $(window).load() version - you've just created a "void" expression that evaluates to a reference to that function.

Do either:

$(window).load(function() {
     loadScript();  // NB: parentheses
})

or:

$(window).load(loadScript);

That said, you perhaps want $(document).ready() rather than $(window).load()

Sign up to request clarification or add additional context in comments.

2 Comments

@Baloo This is because your Jquery isn't loaded. Just try alert(typeof $) and you'll see that
I get the alert saying it's a function, I assume that means it's loaded. Feel free to tell me if I'm wrong though.
0

in your jquery script you don't invoke the loadScript method... Use:

$( window ).load(function() {
    loadScript();
});

Comments

-1

The $(window).load execute when all DOM is ready including images. That means if our web page has completely loaded including image then $(window).load function will call.

$(window).load(function() {

 // script

})

for more detail about $(document).ready vs $(window).load vs window.onload click on

http://www.delhincrjob.com/blog/what-is-document-ready-vs-window-load-vs-window-onload/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.