简体   繁体   English

如何使页面首先加载javascript,然后仅加载php?

[英]How to make page load javascript first, then only load php?

<script type="text/javascript">
function run()
{
  var paper = Raphael( $('.wrapper')[0], 600, 600 ),

       path = paper.path( Raphael.transformPath(pdefs[useDef].path, pdefs[useDef].transform) )
                   .attr( 'stroke-width', 10 )
                   .attr( 'stroke', 'rgb(80,80,80)' ),

    $shim = $('<div id=\'shim-1\'><img src=images/buttons/photo.png></div>') //How to use php assign this line?
}

$(function() {
    run();
});
</script>

I want to use php to write a line of code above : $shim = $('<div id=\\'shim-1\\'><img src=images/buttons/photo.png width=75px height=75px></div>') . 我想用php在上面写一行代码: $shim = $('<div id=\\'shim-1\\'><img src=images/buttons/photo.png width=75px height=75px></div>') Can I write code like below ? 我可以编写如下代码吗? :

    <script type="text/javascript">
    function run()
    {
      var paper = Raphael( $('.wrapper')[0], 600, 600 ),

           path = paper.path( Raphael.transformPath(pdefs[useDef].path, pdefs[useDef].transform) )
                       .attr( 'stroke-width', 10 )
                       .attr( 'stroke', 'rgb(80,80,80)' ),
    </script>

    <?php
     Use mysql get data from database...
     if (condition) {
    ?>
     <script type="text/javascript"> $shim = $('<div id=\'shim-1\'><img src=images/buttons/photo.png width=75px height=75px></div>') </script>
    <?php
     }
    ?>
    <script type="text/javascript">
    }

    $(function() {
        run();
    });

    </script>

But I think the page will load the php first, it mean $shim will be assigned first, then only start executing javascript function run() . 但是我认为该页面将首先加载php,这意味着$shim将首先被分配,然后才开始执行javascript function run() Thus, $shim will be outside the javascript function run() , how to use php assign $shim into the javascript function run() ? 因此, $shim将不在javascript function run() ,如何使用php将$shim分配给javascript function run()

I found out what is the actually. 我发现实际上是什么。 The problem is actually I close javascript tag </script> before I start php tag <?php . 问题实际上是我在启动php标签<?php之前关闭了javascript标签</script> Actually I can start php tag directly without closing javascript tag. 实际上,我可以直接启动php标签,而无需关闭javascript标签。 For example, <script type="text/javascript"> Java codes here... <?php php codes here.... ?> 例如, <script type="text/javascript"> Java codes here... <?php php codes here.... ?>

<script type="text/javascript">
function run()
    {
       var paper = Raphael( $('.wrapper')[0], 600, 600 ),

               path = paper.path( Raphael.transformPath(pdefs[useDef].path, pdefs[useDef].transform) )
                           .attr( 'stroke-width', 10 )
                           .attr( 'stroke', 'rgb(80,80,80)' ),

        <?php
         Use mysql get data from database...
         if (condition) {

     $shim = $('<div id=\'shim-1\'><img src=images/buttons/photo.png width=75px height=75px></div>') 
        <?php
         }
        ?>

        }

        $(function() {
            run();
        });
</script>

How to call this problem actually? 怎么称呼这个问题呢? I think this problem is not about php load before javascript... 我认为这个问题与javascript之前的php加载无关...

Please consider what roughly happens: 请考虑大致发生的情况:

  • The browser (client) sends a request for some URL to the web server over the network. 浏览器(客户端)通过网络向Web服务器发送一些URL请求。
  • The web server runs the PHP script assigned with the URL and generates the HTML page (including JavaScript source, CSS, etc). Web服务器运行分配了URL的PHP​​脚本并生成HTML页面(包括JavaScript源,CSS等)。
  • This pages goes over the network into the client. 该页面通过网络进入客户端。
  • The client parses the HTML, CSS and JavaScript. 客户端解析HTML,CSS和JavaScript。

So JavaScript is executed on the client after PHP has been executed on the server. 因此,在服务器上执行PHP之后,将在客户端上执行JavaScript。

Now to your detailed problem, to me it seems you simply want 现在到您的详细问题,在我看来,您似乎只是想要

function run() {

  var paper = Raphael( $('.wrapper')[0], 600, 600 ),
      path = paper.path(
             Raphael.transformPath(pdefs[useDef].path,
                                   pdefs[useDef].transform))
             .attr( 'stroke-width', 10 )
             .attr( 'stroke', 'rgb(80,80,80)' );

  // string broken into three strings to avoid scrolling 
  $shim = $('<div id=\'shim-1\'>' +
          '<img src=images/buttons/photo.png ' + 
          'width=75px height=75px></div>'); 
}

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

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