I am trying to build a sidebar navigation, which uses AJAX to load content inside a div. So far it is working great, it is just that i noticed, that everytime i click onto the buttons to navigate to a different site on my page, the site content seems to be loaded on top of the current content.
I have got this hypothesis, because on every site load my site sends one request to a server, to get data from there.
Now if i already visited the site on my page, that sends this request, navigate to a different site on my page and navigate back to the first site, this site sends two requests to the server. If i am repeating those steps, it just adds up and the request gets sent 3,4,5..times
This is the Code of my navigation bar:
<div id="sidedrawer" class="mui--no-user-select">
<div class="mui-divider"></div>
<ul>
<li onClick="remote();" href="./#remote">
<strong><a href="./#remote" class="navbarlink">Remote</a></strong>
</li>
<li onClick="groups();" href="./#groups">
<strong><a href="./#groups" class="navbarlink">Groups</a></strong>
</li>
<li onClick="devices();" href="./#devices">
<strong><a href="./#devices" class="navbarlink">Devices</a></strong>
</li>
<li onClick="users();" href="./#users">
<strong><a href="./#users" class="navbarlink">Users</a></strong>
</li>
<li onClick="programs();" href="./#programs">
<strong><a href="./#programs" class="navbarlink">Programs</a></strong>
</li>
<li onClick="settings();" href="./#settings">
<strong><a href="./#settings" class="navbarlink">Settings</a></strong>
</li>
</ul>
</div>
The functions remote()/groups()/devices() look about the same:
function remote() {
showSubPage("Remote", "./remote.php");
}
showSubpage() looks like this:
function showSubPage(title, page){
changeTabName(title);
$.ajax({
url: page,
type: "GET",
cache: false,
success:function(data){
var $main = $('#main');
$main.html('');
$main.html(data);
}
});
}
Main is just a normal div:
<div id="main"></div>
Does anyone have an idea why my page does not get called just once everytime i click the links?
Edit: When i am looking into the source code of my page, the content is only listed once.
Edit2: I figured that the problem does not come from the ajax itself. This is the PHP-code that i am using to generate my page.
function addGroup($groupId, $groupname, $devicearr)
{
echo('<div class="group">
<h3>'.$groupname);
echo('<div class="verticalcenter"><div class="toggle-button toggle-button-selected " id="groupButton'.$groupId.'"><button></button></div></div>');
echo(' </h3>
<div>
<table class="devicetable">
');
foreach ($devicearr as &$device) {
echo('<tr>
<td>'.$device['name'].'</td>');
if($device['status'] == 1){
echo('<td class="togglecolumn"><div class="verticalcenter" ><div class="toggle-button toggle-button-selected group'.$groupId.' device'.$device['id'].'" id="group'.$groupId.'device'.$device['id'].'" ><button></button></div></div></td></tr>');
} else {
echo('<td class="togglecolumn"><div class="verticalcenter"><div class="toggle-button group'.$groupId.' device'.$device['id'].'" id="group'.$groupId.'device'.$device['id'].'"><button></button></div></div></td></tr>');
}
echo ('<script>
$(document).on("click","#group'.$groupId.'device'.$device['id'].'", function() {
if($("#group'.$groupId.'device'.$device['id'].'").hasClass("toggle-button-selected")){
$(".device'.$device['id'].'").removeClass("toggle-button-selected");
var frame = document.createElement("iframe");
frame.src = "http://localhost/callMethod";
frame.style.left = "-9999px";
frame.style.display = "none";
frame.onload = function() {
this.parentNode.removeChild(this);
};
document.body.appendChild(frame);
}
else{
$(".device'.$device['id'].'").addClass("toggle-button-selected");
var frame = document.createElement("iframe");
frame.src = "http://localhost/callMethod";
frame.style.left = "-9999px";
frame.style.display = "none";
frame.onload = function() {
this.parentNode.removeChild(this);
};
document.body.appendChild(frame);
}
});
</script>');
}
echo('
</table>
</div>
</div>');
}
My code creates a button for each "device" that is in the "devicearr". But when my page got called twice and i click the button once, it registers two click events.