Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys I want to know how I can allow JS to be enable effectively on each ajax requested page.

These are my files I need to allow JS to work thru.

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>*/	
   
    /*</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>


page_2.php

<script>

var execute_sendAjax2 = document.getElementById('executeAjax2');
  execute_sendAjax2.addEventListener('click', sendAjax2);
  
function sendAjax2(){
var xhr2= new XMLHttpRequest();

xhr2.onreadystatechange = function(){
	if(xhr2.readyState === 4){
		document.getElementById('ajax2').innerHTML= xhr2.responseText;
	
	/*<Allow JS on the requested page>*/	
      
    /*</Allow JS on the requested page>*/
	
	}
}
	xhr2.open('POST','page_3.php');
	xhr2.send();
}

</script>

<button id='executeAjax2'>Execute 2</button>

<h1>Page 2</h1>

<div id='ajax2'></div>


page_3.php

<script>

var execute_sendAjax3 = document.getElementById('executeAjax3');
  execute_sendAjax3.addEventListener('click', sendAjax3);
  
function sendAjax3(){
var xhr3= new XMLHttpRequest();

xhr3.onreadystatechange = function(){
	if(xhr3.readyState === 4){
		document.getElementById('ajax3').innerHTML= xhr3.responseText;
		
	/*<Allow JS on the requested page>*/	
     
    /*</Allow JS on the requested page>*/
		
	}
}
	xhr3.open('POST','page_4.php');
	xhr3.send();
}

</script>

<button id='executeAjax3'>Execute 3</button>

<h1>Page 3</h1>

<div id='ajax3'></div>


page_4.php

<h1>Page 4</h1>


What I have tried:

I have looked online for a long time on this topic not a lot of easy to understand sources.
Posted
Updated 14-May-18 1:58am
Comments
Mohibur Rashid 13-May-18 21:49pm    
Quick answer: you need to append each returned value to some div or another block type element, you are sending request but you are not processing the returned value.

Long answer: I don't have time now, sorry.
Short answer instead of long answer: this is not a very good model. You need to think of a better way of handling.
F-ES Sitecore 14-May-18 4:34am    
Page2

var execute_sendAjax2 = document.getElementById('executeAjax2');

the element "executeAjax2" doesn't exist yet so sendAjax2 will be null. Move the executeAjax2 button above the script. This works on the first page because on the first page you're using the page load event so the code runs after the html has been processed.

1 solution

To prevent you from going down another dead end (after following the previous answers and comments).

If you return a javascript to the page via AJAX it will not be executed directly. The best route you can do for this, keeping it simple, is to have the javascript execute as a pre-existing function call following the AJAX return in the AJAX call. You can change the contents of the function that will execute (via AJAX) and it will execute. You can have a do-nothing function, too, but don't leave it non-existent.

This will save you much frustration later on.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900