So i am developing a website with many html pages and now i have a problem with JavaScript that really puzzles me. The thing is my website locally works just fine, but when i host it on a server some JavasScript code stops to work(sometimes it works) and the interesting thing about it that when inspecting it i don't get any console JS errors and what more interesting is that JS that doesn't work is in the same .js file with other js codes that work.Also, locally everything works 100%. Well the problem was that my mobile media query dropwdown menu JS stopped wroked when i hosted it on the server - originally the code looked like this:
$(function(){
$(document).mousedown(function(){
$('.dropdown .active').removeClass('active').children('ul').hide();
});
$('.dropdown').on('mousedown','.subMenu', function(e){
e.stopPropagation();
var elem = $(this);
if(elem.is('.active')) {
elem.children('ul').slideUp(150);
elem.removeClass('active');
} else {
$('.dropdown .active').removeClass('active').children('ul').hide();
elem.addClass('active').children('ul').slideDown(150);
}
});
$('.subMenu').on('mousedown','ul', function(e){
e.stopPropagation();
});
});
$("#footer").load("footer.html");
$("#header").load("header.html");
As you can see i am using separate footer and header html files and uploading them to my pages using JS. <div id="header"></div>
This worked perfectly locally, but when i hosted it to server my mobile dropdown sometimes works and sometimes doesn't... It looked that i managed to make the javascript work with a better success rate by editing it to this:
$(document).ready(function() {
$(function(){
$(document).mousedown(function(){
$('.dropdown .active').removeClass('active').children('ul').hide();
});
$('.dropdown').on('mousedown','.subMenu', function(e){
e.stopPropagation();
var elem = $(this);
if(elem.is('.active')) {
elem.children('ul').slideUp(150);
elem.removeClass('active');
} else {
$('.dropdown .active').removeClass('active').children('ul').hide();
elem.addClass('active').children('ul').slideDown(150);
}
});
$('.subMenu').on('mousedown','ul', function(e){
e.stopPropagation();
});
});
});
$("#footer").load("footer.html");
$("#header").load("header.html");
And one more important thing - every html file(execpt index.html) has an internal javascript code that gives a certain ID element a class which highlights the page's name(button) on which the user is on. It looks like this.:
<script>
$("#header").load("../pages/header.html", function() {
document.getElementById('A_57').className = 'about';
document.getElementById('A_577').className = 'about';
});
$("#footer").load("../pages/footer.html", function() {
});
</script>
In this case It targets two ID's because one ID is in a UL that only apears in mobile view and the other UL only apears in all the other media queries, but that's not important.
The important thing is to come up with a solution where all 3 javascript codes work together in harmony on a server. Also would be nice to hear while locally everything works fine while on a server it appears that Javascript code lunching numeration differs ...
UPDATE : I can confirm that when i reload a page that i was on it 100% of the time results of a dropdown menu stopping to work. Also from browser inspect>network>html i can see that 1xheader.html and 1xfooter.html turns from 200(when dropdown menu works) to 304(dropdown menu stops working).