0

i click on a button that calls a PHP File over a XMLHttpRequest.

In this PHP File a write a new Javascript Function.

The Result of the PHP File is then loaded on a div container on my first page from where a have clicked the button.

If i click then another button to call the new written Js Function i have no results. (Maybe it isn't defined on that page because of the XMLHttpRequested PHP File)

How can i write this function over a XMLHttpRequested PHP File and call after that this function ???

Thank You for help !!!

here the code from first page:

<html>
  <head>
    <script type="text/javascript">
    function createfunc()
      {
      if(window.XMLHttpRequest)
        {
        RNG_Option = new XMLHttpRequest();
        }
      else
        {
        RNG_Option = new ActiveXObject("Microsoft.XMLHTTP");
        }
      RNG_Option.onreadystatechange=function()
        {
        if(RNG_Option.readyState==4 && RNG_Option.status==200)    
          {       
      document.getElementById("phprequest").innerHTML=RNG_Option.responseText;
          }    
        }
      RNG_Option.open("GET","create.php", true);
      RNG_Option.send();
      }         
    </script>       
  </head>    
   
  <body>
    <a onclick="createfunc();">Create Function</a>
    <div id="phprequest"></div>
    <a onclick="callfunc();">Call Function</a>  
  </body>
</html>

the php file create.php :

<?php   
echo "<script type='text/javascript'>";   
echo "function callfunc()";    
echo "{";   
echo "alert('CALL MY FUNCTION');";   
echo "}";   
echo "</script>"; 
?>
sem
  • 1
  • 1
  • 1
    Welcome to SO. Please show us some relevant code , in particular how you process this function text when you receive it. For reference take a few minutes to read through [ask] and [mcve] – charlietfl Jun 30 '20 at 23:46
  • I added the code – sem Jul 01 '20 at 00:17

1 Answers1

-1

I am using fetch for ajax request.

temp.php:

<?php
echo "function callfunc()";
echo "{";
echo "alert('CALL MY FUNCTION');";
echo "}";

test.php:

<!DOCTYPE html>
<html lang="en">
<body>
    <a onclick="createfunc();">Create Function</a>
    <div id="phprequest"></div>
    <a onclick="callfunc();">Call Function</a>
    <script type="text/javascript">
        function createfunc() {
            fetch('temp.php')
                .then(res => res.text()
                    .then(data => {
                        let script_tag = document.createElement('script');
                        script_tag.type = 'text/javascript';
                        script_tag.text = data;
                        // document.body.appendChild(script_tag);
                        document.querySelector('#phprequest').appendChild(script_tag);
                    }));
        }
    </script>
</body>
</html>

If you want to add JS to current page, you need to get that JS as text, create a script element, add that JS to script element and then add that script element to your page's body or into desired element.

Umair Khan
  • 1,684
  • 18
  • 34