2

I work on asp.net mvc5 project.

I have this code:

//Load Bootstrap JS
var plugin_path = 'assets/plugins/'
loadScript(plugin_path + 'bootstrap/js/bootstrap.min.js', function() {

    Init(false);

});

And this:

var _arr    = {};
function loadScript(scriptName, callback) {
if (!_arr[scriptName]) {
    _arr[scriptName] = true;

    var body        = document.getElementsByTagName('body')[0];
    var script      = document.createElement('script');
    script.type     = 'text/javascript';
    script.src      = scriptName;
    script.onload = callback;
    body.appendChild(script);
} else if (callback) {
    callback();
}
};

when my application starts on landings page evrything works perfect.

When I move to another page, on this row:

body.appendChild(script);

I get this error:

http://localhost:111/Home/assets/plugins/bootstrap/js/bootstrap.min.js 

Application try to search this file:

bootstrap.min.js 

on this path:

http://localhost:111/Home/assets/plugins/bootstrap/js/

While on landing page this file bootstrap.min.js has been searched in this path:

http://localhost:111/assets/plugins/bootstrap/js/bootstrap.min.js 

and evrythng works fine.

So I gess the problem with searched path.

Any idea why searched path is changed when I move to another page?

Michael
  • 13,950
  • 57
  • 145
  • 288
  • How are you constructing the `scriptName` variable for `src`? It should reference the "root" path. The `Home` part in your sample is the `controller` name that is part of the default MVC route scaffolding. While you can adjust your route config to remove the controller name - that wouldn't address the core issue (breaks on any new folder/path structure you create). Hth. – EdSF Feb 01 '17 at 15:30
  • @EdSF I updates the question please see update – Michael Feb 01 '17 at 15:45
  • End of day, check whatever your code ends up resolving to (e.g. what is `plugin_path`?). Whatever it is, It should always end up something like `/assets/plugins/bootstrap/js/bootstrap.min.js` as indicated in your "correct" result - re: path reference starts at `root` of your application folder structure. You're likely using a _relative path_ which, as the term implies, looks for resources _relative_ to the document being requested. – EdSF Feb 01 '17 at 15:51

1 Answers1

1

You need to assign path like that (should start from /):

//Load Bootstrap JS
var plugin_path = '/assets/plugins/'

@PeeHaa wrote the best explanation about this problem: https://stackoverflow.com/a/21828923/1954204

Community
  • 1
  • 1
Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36