简体   繁体   English

ajax调用后,php echo javascript函数不起作用?

[英]php echo javascript function after an ajax call doesnt work?

So I ran into a problem and I couldn't really find a good solution anywhere. 所以我遇到了一个问题,我无法在任何地方找到一个好的解决方案。 And I'm sure more people run into this problem. 我相信更多的人会遇到这个问题。

I tried to have an Ajax script call to a php-script which echoes a JavaScript function again but this function wont run or activate. 我尝试对php脚本进行Ajax脚本调用,该脚本再次回应JavaScript函数,但此函数不会运行或激活。 It however does show up in the code if you do inspect element. 但是,如果你检查元素,它会显示在代码中。

So the html and Ajax is as follows. 所以html和Ajax如下。 Its dummy code since my own is a bit more complicated. 它自己的虚拟代码有点复杂。 so any syntax errors I made here are not the solution since 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 php code that gets called is very simple. 被调用的PHP代码非常简单。

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

So I am looking for a solution which makes me able to run a JavaScript or jquery function after an Ajax call, or the main reason why this doesn't work. 所以我正在寻找一种解决方案,使我能够在Ajax调用之后运行JavaScript或jquery函数,或者说这不起作用的主要原因。 Since I couldn't find it. 因为我找不到它。

Inb4 why call the alert via php? Inb4为什么通过php调用警报? Because I need to verify something first with the db on the server-side in my actual code. 因为我需要在实际代码中首先使用服务器端的db验证某些内容。

So after combining and testing some of the comments I figured out my own answer. 所以在结合并测试了一些评论后,我想出了自己的答案。 You cant create new javascript within the php echo. 你不能在php echo中创建新的javascript。 You can however echo an onload that calls an existing function. 但是,您可以回显调用现有函数的onload。 Onload only works for the following tags: Onload仅适用于以下标记:

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

However in this case after testing some of them like "script" and "img" it still didn't work with all tags, but it did with the "style" tag. 然而,在这种情况下测试其中一些像“脚本”和“img”之后,它仍然不适用于所有标签,但它使用“样式”标签。 I didnt test all other tags though. 我没有测试所有其他标签。

So that changed my code to: 所以这改变了我的代码:

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

and the php-code will then become 然后php代码就会变成

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

and now it does run the function. 现在它确实运行该功能。


edit this doesn't seem to work for IE, looking for a solution right now. 编辑这似乎不适用于IE,现在正在寻找解决方案。

^ EDIT: By default, IE Browsers "DENY" the ability of scripts to throw prompts. ^编辑:默认情况下,IE浏览器“拒绝”脚本抛出提示的能力。 So to enable this functionality, you must go to [Tools/ Internet Options/ Security / Custom Level / "Allow websites to prompt for information using scripted windows"] and enable that... Once you refresh, you will see your alert in IE :) 因此,要启用此功能,您必须转到[工具/ Internet选项/安全/自定义级别/“允许网站使用脚本化窗口提示信息”]并启用...一旦刷新,您将在IE中看到您的警报:)

只需在单引号前添加斜杠,如下所示

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

You could use eval() to evaluate the returned javascript. 您可以使用eval()来评估返回的javascript。 The script tags wont be required if this method is used. 如果使用此方法,则不需要脚本标记。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

Try this: 尝试这个:

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

Basically the reason why nothing happens is because you are just sticking content in the DOM. 基本上没有任何反应的原因是因为你只是在DOM中粘贴内容。 Javascript is an event driven language and since nothing is telling the javascript to run at this point, its just sitting there doing nothing. Javascript是一种事件驱动的语言,因为没有任何东西告诉javascript在这一点上运行,它只是坐在那里什么都不做。 If that code were there when the browser loaded the page, then the browser parsing the code is what would tell it to run. 如果在浏览器加载页面时存在该代码,则解析代码的浏览器将告诉它运行。 So, what you need to do is evaluate any scripts that come back 因此,您需要做的是评估返回的任何脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM