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

Dar*_*rio 1 php jquery magento

我想知道是否有一种方法只在文档准备就绪时加载脚本的一部分,我的意思是当所有的css,js和图像都被下载时运行有点重的php脚本.

在PHP我知道可以使用if if但是在jquery上?

可: ?

        <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>
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

War*_*0ck 6

我认为语言混乱.PHP是一种服务器端语言.它在原始资源触及您的浏览器时已经运行并完成.另一方面,JavaScript在客户端解析并在浏览器中呈现.

如果您需要在页面加载后运行PHP脚本,则需要使用AJAX.您必须执行以下操作:

$(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
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

参考: .ajax()

编辑:

建议用户3553531

由于您要等待加载所有样式表,脚本和图像,我已更新我的答案以使用load事件而不是DOMContentLoaded(后面的事件$(document).ready).

根据下面的MDN文档,该load事件似乎比DOMContentLoaded事件更能满足您的问题要求.

参考: DOMContentLoaded

摘抄:

在完全加载和解析文档时触发DOMContentLoaded事件,而不等待样式表,图像和子帧完成加载(加载事件可用于检测完全加载的页面).