user5381217
user5381217

Reputation:

Pagination AJAX, need to include js all the time?

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>";
    }
?>

Upvotes: 1

Views: 99

Answers (2)

Himanshu
Himanshu

Reputation: 281

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

Upvotes: 1

Julio Soares
Julio Soares

Reputation: 1190

Are you giving an unique id to the containers or elements of the pictures you load in LOADMORE.PHP ? If you do so and they make sense for your js in PAGE.PHP... should just work

Upvotes: 0

Related Questions