Search code examples
jquerygetscriptjsapi

JQuery - getScript opens in a new tab



i have a "imho" strange problem with the jquery getScript function.

$.getScript('jquery_ui.js', function (){});
// this code works fine

<script type="text/javascript" src="http://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22feeds%22%2C%22version%22%3A%221%22%7D%5D%7D"></script>
// works fine,too

$.getScript('http://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22feeds%22%2C%22version%22%3A%221%22%7D%5D%7D', function (){});
// This code redirect my page.
// But why, oO? This is the same like <script type="text/javascript" src="http://www.google.com/j ...

How can i fix this bug?

Thanks in advance
Peter


Solution

  • Defining a callback value for each of the modules you are loading should solve the problem. (Note that the value of autoload in the querystring below contains the names of the modules you're loading and the names of your callbacks.)

    function maps(){
      console.log("maps loaded");
    }
    function feeds(){
      console.log("feeds loaded");
    }
    $.getScript("https://www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22maps%22%2C%22version%22%3A%222%22%2C%22callback%22%3A%22maps%22%7D%2C%7B%22name%22%3A%22feeds%22%2C%22version%22%3A%221%22%2C%22callback%22%3A%22feeds%22%7D%5D%7D");
    // https://www.google.com/jsapi?autoload={"modules":[{"name":"maps","version":"2","callback":"maps"},{"name":"feeds","version":"1","callback":"feeds"}]}