Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I been researching for a long time and i'm so confused I read that you have to use the eval() function so plain ajax can recognize and execute the JavaScript on the other page I read thru a lot of sites but there is rarely no info on this or no examples that makes sense to me so can any one add the right code to my code to get this to work. I'm a visual learner.


index.php


HTML
<script>
var xhr= new XMLHttpRequest();
xhr.onreadystatechange= function(){
    if(xhr.readyState === 4){
        document.getElementById('ajax').innerHTML= xhr.responseText;
    }
};
xhr.open('POST','x.php');

function startAjax(){
    xhr.send();
document.getElementById('hide_button').style.display= 'none';
    }
    </script>
    <body>
    <button id='hide_button' onclick='startAjax()'>Start</button>
    <div id='ajax'></div>
    </body>


x.php


HTML
<script>
alert('js is executed');
</script>

<h1>Radom text</h1>


What I have tried:

I keep asking on many sites but nobody wants to help me out it's like they don't know how to do this or They don't modify my code to show how this can be done. The code works it's just not executing the js on the other page call x.php.
Posted
Updated 5-Jan-18 15:10pm

1 solution

refer this
Page1
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script>
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                var response = xhr.responseText; 
                document.getElementById('ajax').innerHTML = response;
                setTimeout(function () {
                    var script = document.getElementById('ajax').getElementsByTagName('script')[0];
                    if (script) {
                        var js = script.innerText;
                        eval(js);
                    } 
                }, 100)
                
            }
        };
        xhr.open('get', 'htmlpage2.html');

        function startAjax() {
            xhr.send();
            document.getElementById('hide_button').style.display = 'none';
        }
    </script>
</head>
<body>
    <button id='hide_button' onclick='startAjax()'>Start</button>
    <div id='ajax'></div>
</body>


</html>


Page2
<script>
    alert('js is executed');
</script>

<h1>Radom text</h1>
 
Share this answer
 
Comments
Member 13605567 5-Jan-18 21:43pm    
Thank you so much Karthik Bangalore I been trying to figure this out for a very long time. Your the only one that knows what I was looking for thanks.
Karthik_Mahalingam 5-Jan-18 21:48pm    
welcome, btw delete the old question.Ajax does not execute other page javascript[^]
Member 13605567 6-Jan-18 0:14am    
Ok I will but how can I make this execute multiple script tags? It only execute one script tag section.

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