如何让JS在每个ajax请求的页面上工作? [英] How can I allow JS to work on each ajax requested page?

查看:86
本文介绍了如何让JS在每个ajax请求的页面上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我想知道如何在每个ajax请求的页面上有效地启用JS。



这些是我需要允许JS的文件工作直到。



page_1.php



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





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





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





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.

推荐答案

为了防止你走下另一个死胡同(在听完之前的答案和评论之后)。



如果您通过AJAX将javascript返回到页面,则不会直接执行。为此,你可以做的最好的路线就是让javascript在AJAX调用中的AJAX返回之后执行为预先存在的函数调用。您可以更改将执行的函数的内容(通过AJAX),它将执行。你也可以有一个无所事事的功能,但不要让它不存在。



这将为你以后节省很多挫折。
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.


这篇关于如何让JS在每个ajax请求的页面上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆