Php echo javascript function after ajax call not working?

So, I had a problem, and I could not find a good solution anywhere. And I'm sure more people are facing this problem.

I tried to call an Ajax script call to php-script, which again repeats the JavaScript function, but this function does not start or activate. However, it appears in the code if you check the item.

So html and Ajax are as follows. Its a dummy code since my own is a bit trickier. therefore, any syntax errors that I have made here are not a solution, as this works for other parts of my code.

<html><headbodyetc>
    <div id='change'>
        //is supposed to alert or call another js function after 
        //verifying something with a database for instance.
        <button type='button' onclick='ajaxbelow();'>alert</button>
    </div>
    <script type='text/javascript'>
        function ajaxbelow(){
        //AJAX code as found on w3schools.com/php/php_ajax_php.asp
        //calls the change.php
    </script>
</etc></html>

The resulting php code call is very simple.

//This shows up in the html-code after clicking the button but doesnt run.
echo"<script type='text/javascript'>alert('doenst work?')</script>";

, , JavaScript jquery Ajax , . .

Inb4 php? - db .

+4
4

, . javascript php-. , , . Onload :

"body", "frame", "frameset", "iframe", "img", "input type =" image "," link "," script "," style ".

, "script" "img", - , "style". .

, :

<html><headbodyetc>
<div id='change'>
    //is supposed to alert or call another js function after 
    //verifying something with a database for instance.
    <button type='button' onclick='ajaxbelow();'>alert</button>
</div>
<script type='text/javascript'>
    //function to be called
    function test(){
        alert("now it works");
    }
    function ajaxbelow(){
    //AJAX code as found on w3schools.com/php/php_ajax_php.asp
    //calls the change.php
</script>
</etc></html>

php-

echo"<style onload='test();'></style>";

.


, , IE, .

^ EDIT: IE "DENY" . , [/ // / " - " ] ... IE:)

+2

,

   <?php echo '<script type="text/javascript">alert(\'doenst work?\')</script>'; ?>
0

:

http://www.webdeveloper.com/forum/showthread.php?184830-Ajax-echo-script-problem

Basically, the reason why nothing happens is because you just paste the content into the DOM. Javascript is an event-driven language, and since nothing says javascript will work at this point, it just sits there, doing nothing. If this code was there when the browser loaded the page, then the browser analyzing the code would be what would tell it to start. So what you need to do is evaluate all returned scripts.

-1
source

Source: https://habr.com/ru/post/1534395/


All Articles