【问题标题】:How to make page load javascript first, then only load php?如何让页面先加载javascript,然后只加载php?
【发布时间】:2015-09-11 22:47:51
【问题描述】:
<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>

我想用php在上面写一行代码:$shim = $('&lt;div id=\'shim-1\'&gt;&lt;img src=images/buttons/photo.png width=75px height=75px&gt;&lt;/div&gt;')。我可以编写如下代码吗? :

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

但我认为页面会先加载php,这意味着$shim将首先分配,然后才开始执行javascriptfunction run()。这样,$shim就会在javascriptfunction run()之外,如何使用php将$shim赋值给javascriptfunction run()呢?

我发现了实际情况。问题实际上是我在启动 php 标签&lt;?php 之前关闭了javascript 标签&lt;/script&gt;。其实我可以直接启动 php 标签而不用关闭 javascript 标签。例如,&lt;script type="text/javascript"&gt; Java codes here... &lt;?php php codes here.... ?&gt;

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

实际上如何调用这个问题?我认为这个问题与javascript之前的php加载无关......

【问题讨论】:

  • PHP 在网络浏览器获取页面源代码之前运行。因此,这个问题中的 PHP 是没有根据的;只有 HTML(带有 JS)。要推迟 PHP 本身(如果确实是“实际问题”),它必须在 单独 请求中处理 - XHR、带参数的回发等。
  • 大胆猜测:您是否正在寻找发送 AJAX 请求?即 PHP 加载页面,发送到浏览器 > javascript 在浏览器上执行 > 向服务器 (PHP) 发送 ajax 请求 > 将数据发送回 javascript,您可以随意使用它。

标签: javascript php jquery mysql raphael


【解决方案1】:

请考虑一下大概会发生什么:

  • 浏览器(客户端)通过网络向 Web 服务器发送对某个 URL 的请求。
  • Web 服务器运行分配有 URL 的 PHP 脚本并生成 HTML 页面(包括 JavaScript 源、CSS 等)。
  • 此页面通过网络进入客户端。
  • 客户端解析 HTML、CSS 和 JavaScript。

因此,在服务器上执行 PHP 之后,在客户端上执行 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)' );

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

【讨论】:

  • 如何使用php将$shim = $('&lt;div id=\'shim-1\'&gt;&lt;img src=images/buttons/photo.png width=75px height=75px&gt;&lt;/div&gt;')赋值给javascriptfunction run()
  • @zac1987 我不清楚这是应该在生成的网页中显示的最终 JavaScript 代码(在这种情况下将该字符串放入 php 文件中)还是 PHP 必须这样做与那句话有关的东西。
  • 因为我想让 html div 变得可拖动。我用 jquery-ui raphael $shim.draggable({ start: function(e){}
  • 是的,我已经解决了。我已经编辑了我的帖子以显示解决方案。谢谢。
猜你喜欢
  • 2014-09-08
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多