Here's a situation I'm facing - every page involves an ajax request. I managed to enable JavaScript to run on each ajax requested page. Out of curiosity, I started testing my code by opening Chrome's developer tools to peek behind the scenes.
I discovered some glitches, where pressing the button on page 1 triggers the same amount of requests as the number of times the button is pressed. However, when I try this on page 2 or 3, it results in hundreds of additional ajax requests within a short time frame. This behavior needs to be fixed. While I want JavaScript enabled on all pages, I need to prevent multiple ajax requests and ensure that the button's actions correspond appropriately.
Take a look at this .gif screenshot to understand better: https://i.sstatic.net/5LR5O.gif
Below are the code files:
page_1.php
<script>
document.addEventListener('DOMContentLoaded', function(){
var execute_sendAjax1 = document.getElementById('executeAjax1');
execute_sendAjax1.addEventListener('click', sendAjax1);
function sendAjax1(){
var xhr1= new XMLHttpRequest();
xhr1.onreadystatechange = function(){
if(xhr1.readyState === 4){
document.getElementById('ajax1').innerHTML= xhr1.responseText;
/*<Allow JS on the requested page>*/
var exJs = document.getElementsByTagName('script');
var enableAll = null;
for (var i = 0; i < exJs.length; i++) {
enableAll += exJs[i].innerHTML;
}
eval(enableAll);
/*</Allow JS on the requested page>*/
}
}
xhr1.open('POST','page_2.php');
xhr1.send();
}
});
</script>
<button id='executeAjax1'>Execute 1</button>
<h1>Page 1</h1>
<div id='ajax1'></div>
...
This type of issue can lead to browser freezes, which is not ideal. Thus, I am seeking a more efficient method to allow JavaScript while preventing the creation of excess ajax requests after multiple button clicks. Page 1 operates as intended, avoiding the cascade of requests seen on page 2 and 3. Identifying a solution to address this anomaly is crucial to maintain a seamless browsing experience.