Michael
Michael

Reputation: 13614

Why I get error js file could not be found?

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?

Upvotes: 2

Views: 145

Answers (1)

Victor Leontyev
Victor Leontyev

Reputation: 8736

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

Upvotes: 1

Related Questions