简体   繁体   English

准备好在Jquery文档上运行php脚本

[英]Run php script on Jquery document ready

I would like to know if is there a way to load a part of script only when the document is ready, i mean when all css, js, and images are downloaded Run the php script that is a bit heavy. 我想知道是否只有在文档准备好时才加载脚本的一部分的方法,我的意思是当所有css,js和图像都下载后,运行有点沉重的php脚本。

in php i know that can be use an if else but on jquery ? 在PHP中,我知道可以在jQuery上使用if else吗?

can be: ? 可: ?

        <script>
        jQuery( document ).ready(function( $ ) {
        </script>
        <?php
         $script = $this->getAllItems();
        ?>
        <script>
        });
          </script>



    <div class="page">
            <?php echo $this->getChildHtml('header'); ?>
            <div class="main-container col2-left-layout">
                <div class="main container_16 clearfix">
                    <div class="col-main grid_16">
                        <?php echo $this->getChildHtml('top_callout'); ?>
                    </div>
                    <div class="col-main grid_16">
                        <?php echo $this->getChildHtml('breadcrumbs'); ?>
                    </div>
                    <div class="col-main grid_12">
                        <?php echo $this->getChildHtml('global_messages'); ?>


on document ready call content
                        <?php echo $this->getChildHtml('content'); ?>



                    </div>
                    <div class="col-left sidebar grid_4"><?php echo $this->getChildHtml('left'); ?></div>
                </div>
            </div>
            <?php echo $this->getChildHtml('footer'); ?>
            <?php echo $this->getChildHtml('before_body_end'); ?>
        </div>

Any help is appreciated ! 任何帮助表示赞赏!

I think there is a language confusion. 我认为这是一种语言混乱。 PHP is a server-side language. PHP是一种服务器端语言。 It has already run and completed by the time the raw source touches your browser. 到原始源代码触摸您的浏览器时,它已经运行并完成。 JavaScript on the other hand is client-side parsed and rendered in the browser. 另一方面,JavaScript是在浏览器中进行客户端解析和渲染的。

If you need to run a PHP script after a page loads, you'll need to make use of AJAX. 如果需要在页面加载后运行PHP脚本,则需要使用AJAX。 You'll have to do something like the following: 您将必须执行以下操作:

$(window).load(function () {
    $.ajax({
        url: "some-php-script.php",
        data: { ... } // data to send to php if applicable,
        ... // other options that may be needed
        success: function (res) {
            ... // handle successful return of data (if applicable)
        }
        error: function (res) {
            ... // handle error if appropriate
        }
    });
});

Reference: .ajax() 参考: .ajax()

EDIT: 编辑:

Suggested by user3553931 由user3553931建议

Since you're wanting to wait for all stylesheets, scripts and images to load, I've updated my answer to use the load event instead of DOMContentLoaded (the event behind $(document).ready ). 由于您要等待所有样式表,脚本和图像加载,因此我更新了答案以使用load事件而不是DOMContentLoaded$(document).ready背后的事件)。

Per the MDN documentation below, it seems that the load event will fulfill your question requirements better than the DOMContentLoaded event. 根据下面的MDN文档,看来load事件比DOMContentLoaded事件更能满足您的问题要求。

Reference: DOMContentLoaded 参考: DOMContentLoaded

Excerpt: 摘抄:

The DOMContentLoaded event is fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading (the load event can be used to detect a fully-loaded page). 当文档已完全加载和解析时,将触发DOMContentLoaded事件,而无需等待样式表,图像和子帧完成加载(可以使用load事件来检测完全加载的页面)。

You'll have to do this via ajax. 您必须通过ajax执行此操作。 By the time your document.ready runs the PHP is long gone. 在您的document.ready运行时,PHP已经不复存在了。 PHP is executed on the server side and JS is executed on the client side. PHP在服务器端执行,而JS在客户端执行。 Try doing something like this: 尝试做这样的事情:

<script>
(function($) {
    $.ajax({
        type: "POST",
        url: 'index.php',
        data: { }, //send data if needed
        success:function(){
            alert("Success!");
        }
    });
</script>

Where url is the path to your php script. 其中url是您的php脚本的路径。 You can even return something in that php file and do something with it in the success handler of the ajax call. 您甚至可以在该php文件中返回某些内容,并在ajax调用的成功处理程序中对其进行处理。

For more info on AJAX see here: .ajax() 有关AJAX的更多信息,请参见: .ajax()

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

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