douzhouqin6223 2016-10-04 18:16
浏览 66
已采纳

从php脚本调用JavaScript函数

I'm wondering if this is something I can do. I have a PHP script that needs to call a function that I define in my main index.php.

Inside my other PHP file I have:

echo "<script>ShowModal('PopUpNameItem','closeNameItem');</script>";

and inside my main index.php I have that function. how do I actually call this from a seperate script? Normally you would use something like this but since my files are .php can I do this?

<script type="text/javascript" src="ajax.js"></script>

and this is the function I'm trying to call

function ShowModal(popup, closeBtn) {
        var modal = document.getElementById(popup);
        var span = document.getElementsByClassName(closeBtn)[0];
        modal.style.display = "block";
        span.onclick = function() {
            modal.style.display = "none";
        }
        if (modal != "PopUpNameItem") {
            window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    }
}
  • 写回答

2条回答 默认 最新

  • dongpaocuan7498 2016-10-04 19:54
    关注

    Ok. I think can see the clear picture now.

    No. it is not possible. You can not can not call client-side javascript method from server-side PHP script.

    In web application there are to sides:

    • client (in your case its web browser, that works with html/css and javascript + data received from the server)
    • server side that works with whatever (in your case its PHP) and generates the html/css, javascript and data.

    Now, your index.php on the server side is processed by the web server and produces index.html that is being sent by the server to the client. Client opens this file and loads/executes all the scripts,styles that are defined in it. And it creates document structure. All of it is now existing on the client side.

    However, server side does not have a direct link to your client context. So the are two ways to run this method as a result of the server processing.

    1) to send a ajax request to the server, and, based on the result, execute the method.

    2) If you don't want to deal with ajax cal logic, you may do a script insertion approach. How it works, is that when the button is pressed you do something like this:

    var scriptRef = document.createElement('script');
    scriptRef.setAttribute('src','url_to_script.php');
    document.head.appendChild(scriptRef);
    

    What it will do, it will dynamically append a script tag to your document and load script content as a result of your php script output. There you can decide to launch the method or do something else.

    However, script insertion is a bit of a headache and should not be used on a long-running pages (in my opinion), as it actually adds a new script every time you press a button. And its your responsibility to keep track of it and remove it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?