Friday 29 November 2019

javascript - jQuery blatantly ignoring the path I give to .getScript()



I have a script, TemplateLoader.js which loads 2 Mustache templates, and renders them on the page (or at least that's the goal).



My directory structure:



COMP266
Unit 4
scripts
mustache.min.js

TemplateLoader.js
PageUsingTemplateLoader.html


Inside of TemplateLoader (the object), I have the following chunk to load Mustache, and render the templates:



$.getScript("./scripts/mustache.min.js", function() {
$('head').html( Mustache.render(headTemplate, data) );
$('body').html( Mustache.render(bodyTemplate, data, uniqueBodyTemplate) );
});



This however, yields the following error in the developer console:




HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).
(XHR): GET - http://localhost:63342/COMP266/Unit%204/mustache.min.js?_=1450903391318




Oddly, it seems to have dropped the script folder completely from the path.




I decided to play around, so I changed the fetch path to (duplicating the script folder):



./scripts/scripts/mustache.min.js


But this yields:




HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).

(XHR): GET - http://localhost:63342/COMP266/Unit%204/scripts/scripts/mustache.min.js?_=1450903743022




Now it's listening! Unfortunately, this is obviously the wrong path.



I have no idea how to go about debugging this. It seems like jQuery is selectively dropping the scripts folder. That seems ridiculous, but just to make sure, I searched through the jQuery source, and couldn't find anything that would be doing the observed filtering of the path.



It's currently local, not hosted.



Can anyone give me a hint about what's going on here?



Answer



It turns out the issue was caused by me forgetting that my template added the mustache script (from a previous test), resulting in it being added twice.



I don't understand how this affected it though. It's not like the jQuery addition was succeeding while the template addition was causing the error, since changing the jQuery fetch path caused a direct change in the error message.



Sure enough though, when I removed the script import from the template, it worked.



I really don't understand how this caused a specific folder to be dropped from the path though.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...