【问题标题】:clean url and javascript load order problem in phpphp中的干净url和javascript加载顺序问题
【发布时间】:2011-06-12 11:21:56
【问题描述】:

我正在尝试构建一个 php 网页。packing.php 包含:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="description" content="Fresh Sliding Thumbnails Gallery with jQuery and PHP" />
        <meta name="keywords" content="jquery, images, gallery, full page, thumbnails, scrolling, sliding, php, xml"/>
        <link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/>


<link rel="stylesheet" type="text/css" href="css/ddsmoothmenu.css" />

<script type="text/javascript" src="js/jquery-1.6.1.js"></script>


<link rel="stylesheet" type="text/css" href="css/site.css" />

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

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

<script type="text/javascript" >
$(document).ready( function(){


  // Menu 

  ddsmoothmenu.init({
    mainmenuid: "smoothmenu1", //menu DIV id
    orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
    classname: 'ddsmoothmenu', //class added to menu's outer DIV
    //customtheme: ["#1c5a80", "#18374a"],
    contentsource: "markup" //"markup" or ["container_id", "path_to_menu_file"]
}); 
});

</script>

我的 .htaccess 是

RewriteEngine On
RewriteRule ^packing/Pure packing.php?id=pure
RewriteRule ^packing/ExtraVirgin packing.php?id=EV

现在当我打开我的 php 时,它会正确显示我打开了 packing.php

但是当我使用干净的 URL 打开时,它会在萤火虫中出现错误,说明 $ 未定义。

现在我知道问题是由于 javascripts 的错误加载顺序造成的。 为什么会发生,我该如何解决?

谢谢。

【问题讨论】:

    标签: php javascript jquery clean-urls


    【解决方案1】:

    当您使用干净的 URL 时,浏览器会认为它位于不同的目录 (/packing/pure) 中。指向您的 JS 文件的相对 URL 将不再起作用。

    改用绝对 URL:

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

    【讨论】:

      【解决方案2】:

      路径可能有问题?您没有提供路径,而是将路径从根 (packing.php) 更改为第二级 (packing/Pure)。它可能会在错误的路径中查找您的 .js 文件,例如 packing/js/ 而不仅仅是 /js

      尝试使用绝对路径进行加载:

      <script type="text/javascript" src="/js/jquery-1.6.1.js"></script>
      

      (注意斜线!)
      甚至

      <script type="text/javascript" src="http://yoursite.com/js/jquery-1.6.1.js"></script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-15
        • 1970-01-01
        • 1970-01-01
        • 2017-11-13
        • 1970-01-01
        相关资源
        最近更新 更多