0

I was trying to get the external JS file in my HTML using $.getScript. However it is returning undefined.

Then I tried with https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js that also shows the same, undefined.

$(function() {
  $.getScript("https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", function(data, textStatus, jqxhr) {
    alert(data); // undefined
    alert(textStatus); // Success
    alert(jqxhr.status); // 200
    alert("Load was performed.");
  });
});

Is there any other way we can load the external script?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Nidheesh
  • 4,390
  • 29
  • 87
  • 150
  • 3
    you can use a script tag, like the rest of the world – madalinivascu Jun 04 '19 at 10:08
  • 1
    you are loading a jquery instance using jquery, why? – madalinivascu Jun 04 '19 at 10:09
  • 2
    Are you hitting [same origin policy](https://en.wikipedia.org/wiki/Same-origin_policy) issues? Is the script you're loading hosted on the same domain that the script is running on? – freefaller Jun 04 '19 at 10:10
  • @madalinivascu I was just trying a sample url. Actual url is different one. – Nidheesh Jun 04 '19 at 10:10
  • @freefaller: We are just including the script right? In that case does this same origin matters? Anyway, url is loading when I hit on browser. – Nidheesh Jun 04 '19 at 10:13
  • 1
    Using a ` – freefaller Jun 04 '19 at 10:15
  • @madalinivascu: I used the script tag first. ` ` - like this. But getting window.myfunctionInMyjs is not a function. Means the script is not loaded right? – Nidheesh Jun 04 '19 at 10:36
  • means you need a document ready statement so you execute the code **after all the content has loaded** – madalinivascu Jun 04 '19 at 10:47

1 Answers1

0

After asking the following question in comments...

Are you hitting same origin policy issues? Is the script you're loading hosted on the same domain that the script is running on?

... and the response from the OP...

We are just including the script right? In that case does this same origin matters? Anyway, url is loading when I hit on browser

... leads me to believe the OP is trying to load script from a different domain.

Using the <script> tag does not have same-origin policy constraints, meaning you can load a script from anywhere.

However, the jquery getScript functionality does have the constraint (despite it not being mentioned in the documentation), meaning you can only load script from the same domain (unless you have CORS set up).

freefaller
  • 19,368
  • 7
  • 57
  • 87