doufang6268 2015-10-02 18:01
浏览 30
已采纳

分页AJAX,需要一直包含js吗?

I have page.php with about 10 JS files:

PAGE.PHP

<script type="text/javascript" src="somefile1.js"></script>
<script type="text/javascript" src="somefile2.js"></script>
<script type="text/javascript" src="somefile3.js"></script>
...
<div id="pictures"></div>
...

in this page I have some pictures, that need all this js, and a AJAX function to do an pagination after each 10 pictures:

AJAX

next page ->

$.post('/LOADMORE.php',{
    'group_no': track_load,
    'idpag': idpag
}, 

Ok, when user click in next page the AJAX will call LOADMORE.PHP to load more pictures in div id="pictures"

this pictures need some Jquery (as the pictures above - this jquery functions is in PAGE.PHP already) functions to open pictures bigger in the screen and other things.

What happening is that the scripts in PAGE.PHP does not work with the pictures loaded using AJAX pagination. So I need to include all 10 js files in LOADMORE.PHP too:

<script type="text/javascript" src="somefile1.js"></script>
<script type="text/javascript" src="somefile2.js"></script>
<script type="text/javascript" src="somefile3.js"></script>
<?php
        $stmt = $mysqli->prepare("SELECT * FROM posts ORDER BY id DESC LIMIT ?");
    ...
    while($l = $result->fetch_assoc()) {
    echo"<img src=$picture>";
    }
?>
  • My question 1 is, is it right, do I need to include JS in loadmore.php for this to work?

  • Question 2: if it is right, there is no problem that this <script> in loadmore.php is not on the <head> tag? because the loadmore.php is just a PHP function that select pictures in mysql and bring it to the user, there is no <head><title><body>... should I use this tags?

  • 写回答

2条回答 默认 最新

  • dtihe8614 2015-10-03 02:13
    关注

    I would like to suggest that you should use switch-case for this to work properly. There is no need to include all files in all pages and u don't have to use all that tags in LOADMORE file. Do 1 thing use switch case give page name as case and in that perticular page use all JS files you are desire to. As shown below by using this method you don't have to include unnecessary files in all pages and your code just work fine. For Ex:-

    switch($page)
    {
    case "PAGE":
    <script type="text/javascript" src="somefile1.js"></script>
    <script type="text/javascript" src="somefile2.js"></script>
    <script type="text/javascript" src="somefile3.js"></script>
    break;
    case "LOADMORE":
    <script type="text/javascript" src="somefile4.js"></script>
    <script type="text/javascript" src="somefile5.js"></script>
    <script type="text/javascript" src="somefile6.js"></script>
    break;
    default:
    <script type="text/javascript" src="commonjs1.js"></script>
    <script type="text/javascript" src="commonjs2.js"></script>
    <script type="text/javascript" src="commonjs3.js"></script>
    break;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料