3

I have a requirement of including a .js file (more jQuery code) at a run time in a currently working jQuery script. For example, if my page gets authenticated, then I want to include a particular script file. I want to do this in ASP.Net MVC.

Is there a way to do this? If so, what path do I give when calling that file?

I googled $.getScript('file_path'); but my page is getting refreshed in a loop, I don't why! Maybe I'm doing something wrong in path.

Any help would be much appreciated! Thank you.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Gaurav Gupta
  • 873
  • 2
  • 8
  • 25
  • have a look at require js. http://requirejs.org/ – Strikers Jul 18 '17 at 07:32
  • `$.getScript()` is exactly what you need. We can't help you with the refreshing problem without seeing your code, though. – Rory McCrossan Jul 18 '17 at 07:37
  • @RoryMcCrossan Can you help me with the path which I should provide in it. Like, I've my js file in "Scripts" folder in solution explorer, say content.js, and I want to call it in $.getScript();, what path should I give? like - "~/Scripts/content.js" or something else? – Gaurav Gupta Jul 18 '17 at 07:40
  • 1
    You can't use `~` in a JS code directly, as it's a MVC construct. You need to use a relative path, eg `$.getScript('/basefolder/foo.js');` – Rory McCrossan Jul 18 '17 at 07:43
  • @RoryMcCrossan I've tried the above thing as below : `xhr.onreadystatechange = function (e) { if (xhr.readyState === 4 && xhr.status == 200) { $.getScript(xhr.responseText); } else if (xhr.readyState === 4 && xhr.status == 403) { location.href = '/Home/Auth'; } };` while "xhr.responseText" would reply as "/Scripts/content.js" but no success, I think path is being a issue. – Gaurav Gupta Jul 18 '17 at 08:01
  • @RoryMcCrossan Thank you! Got my code working now.. path set to basefolder i.e, "../../js/foo.js". – Gaurav Gupta Jul 18 '17 at 10:25
  • Glad you got it working. I added an answer for you – Rory McCrossan Jul 18 '17 at 10:27

4 Answers4

0

Try this Including a .js file within a .js file

Basically, create script tag, append it so that it is read before the one that requires it & load the needed .js

Kenan Balija
  • 693
  • 1
  • 7
  • 16
0
//Load JS files as following

["../common/howler.min.js","js/min.js"].forEach(function (src) {
    var script = document.createElement('script');
    script.src = src;
    script.async = false;
    document.head.appendChild(script);
});
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
0

$.getScript() is exactly what you should use for this.

I want to call it in $.getScript();, what path should I give? like - "~/Scripts/content.js" or something else?

The issue there is because the tilde character (~) is an ASP.Net MVC construct which will not work in Javascript. For JS you need to use a relative path, for example:

$.getScript('/basefolder/foo.js');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Added correct path and it worked with "$.getScript();"

Gaurav Gupta
  • 873
  • 2
  • 8
  • 25