0

I have a div that includes shoutbox posts. I'm using jQuery and ajax to change the pages within the div. However, when the page changes, it loses the link to the javascript file so that the next time I try to change the page it actually continues with the link action instead of doing the ajax in the background. Then after that it's back to normal and it alternates back and forth between being linked to the file and not.

Also before, it was rendering the whole page so that my layout was being displayed on the refresh instead of just the shoutbox posts. I'm guessing that finally getting it to refresh without re displaying the whole layout again is what's causing it to lose the connection to the javascript file.

This is the code for the posts. The shoutbox_arrows contains the links to change the page. refresh_me is what I'm loading into my div to refresh the content.

<div id="shoutbox_arrows">
    <?php $current_page=s tr_replace( '?', '@', getURI(fullURL())); ?>
    <ul class="no_dots">
        <li id="first_page"><a href="<?php echo relativeLink('refresh') . '?page=1&redirect=' . $current_page; ?>">&lt;&lt;</a>
        </li>
        <li id="previous_page"><a href="<?php echo relativeLink('refresh') . '?page=' . $previous_page . '&redirect=' . $current_page; ?>">&lt;</a>
        </li>
        <li><strong>Pg#<?php if ($page > $last_page) {echo $last_page;} else {echo $_SESSION['shoutbox_page'];} ?></strong>
        </li>
        <li id="next_page"><a href="<?php echo relativeLink('refresh') . '?page=' . $next_page . '&redirect=' . $current_page; ?>">&gt;</a>
        </li>
        <li id="last_page"><a href="<?php echo relativeLink('refresh') . '?page=' . $last_page . '&redirect=' . $current_page; ?>">&gt;&gt;</a>
        </li>
    </ul>
</div>
<div id="shoutbox" class="custom_scrollbar">
    <div id="refresh_me">
        <?php if (sizeof($shouts)==0 ) { ?>
        <p>There are no posts.</p>
        <?php } foreach ($shouts as $shout) { foreach ($shout as $k=>$v) { $shout[$k] = utf8_encode($v); if ($k == 'guest') { $shout[$k] = ucwords($v); } } ?>
        <div class="post_info">
            <div class="left">
                <?php if ($shout[ 'user_id']==n ull) {echo $shout[ 'guest'];} else { ?><a href="<?php echo relativeLink('users/profile'); ?>?id=<?php echo $shout['user_id']; ?>"><?php echo ucwords(userinfo($shout['user_id'])->username); ?></a>
                <?php } ?>
            </div>
            <div class="right">
                <?php time_format($shout[ 'created_at']); ?>
            </div>
        </div>
        <p class="post_comment" id="shoutbox_comment_<?php echo $shout['id']; ?>">
            <?php echo $shout[ 'comment']; ?>
        </p>
        <?php if (!$shout[ 'last_edited_by']==n ull) { ?>
        <p class="last_edited">Edited by
            <?php echo ucwords(userinfo($shout[ 'last_edited_by'])->username); ?>
            <?php time_prefix($shout[ 'updated_at']); ?>
            <?php time_format($shout[ 'updated_at']); ?>.</p>
        <?php } ?>
        <?php if (current_user()) { if (current_user()->user_id == $shout['user_id'] or current_user()->is_mod) { ?>
        <p class="post_edit">   <span class="edit" id="<?php echo $page; ?>">
                    <a id="<?php echo $shout['id']; ?>" href="<?php $post_to = '?id=' . $shout['id']. '&uid=' . $shout['user_id']; echo $post_to; ?>">
                        edit
                    </a>
                </span> |   <span class="delete" id="<?php echo $page; ?>">
                    <a href="<?php $post_to = '?id=' . $shout['id']. '&uid=' . $shout['user_id']; echo $post_to; ?>">
                        delete
                    </a>
                </span>
    <span class="hide" id="<?php echo $page; ?>">
                    <?php if (current_user()->is_mod) { ?> | <a href="<?php $post_to = '?id=' . $shout['id']. '&uid=' . $shout['user_id']; echo $post_to; ?>">
                        hide
                    </a><?php } ?>
                </span>

        </p>
        <?php }} ?>
        <?php } ?>
    </div>
</div>

This is the page that my ajax request is going to.

<?php

if (isset($data['page'])) {
    $_SESSION['shoutbox_page'] = intval($data['page']);
}

$redirect = ltrim(str_replace('@', '?', $data['redirect']), '/');

redirect_to($redirect);

Div that contains the content to be refreshed.

<div id="shoutbox_container">
    <?php relativeInclude( 'views/shoutbox/shoutbox'); ?>
</div>

jQuery

$('#shoutbox_arrows ul li a').click(function (event) {
    event.preventDefault();
    $.post('views/shoutbox/' + $(this).attr('href'), function (data) {
        $('#refresh_me').load(location.href + " #refresh_me>", "");
        $('#shoutbox_arrows').load(location.href + " #shoutbox_arrows>", "");
    });
});

So I guess to clarify the issue: The shoutbox_container displays posts for the shoutbox. The page is controller by a session that gets passed as a variable to get the correct chunk of posts to show. Clicking on the links in shoutbox_arrows sends an ajax request to a page which changes the session variable. The div that contains the post itself (refresh_me) as well as the arrows (for the links) get refreshed. After changing the page once, the shoutbox is no longer connected to the javascript file so when you click to change the page again, instead of an ajax request, the page itself actually changes to the link.

Any ideas how I can fix this? I've spent a lot of time on this and it's getting rather frustrating. I feel like I could just settle for it as it is now but it's bugging me too much that it's not working exactly how I intend (although generally it works in terms of changing the pages).

Also just a note, I used jsfiddle to tidy up the code but it looks like it did some funky stuff (looking just at $current_page=s tr_replace). lol. So there aren't any syntax errors if that's what you're thinking. >< Also I was going to set up a fiddle but I don't really know how to handle links in it so it would have been useless.

Niketa
  • 120
  • 1
  • 8

2 Answers2

2

The issue is that you bind the click handler to the a tags on document ready (the jQuery code you provided). So when you replace the content of #shoutbox_arrows you remove the click handler you previously attached since those original handlers are removed from the DOM along with the original elements.

You need to use the jQuery .on() method and event bubbling. This will attach the handler on a parent element that will not be removed in your content replace and can continue to "watch" for the event to bubble up from it children elements.

Try replacing your jQuery code with this:

$('#shoutbox_arrows').on('click', 'ul li a', function (event) {
    event.preventDefault();
    $.post('views/shoutbox/' + $(this).attr('href'), function (data) {
        $('#refresh_me').load(location.href + " #refresh_me>", "");
        $('#shoutbox_arrows').load(location.href + " #shoutbox_arrows>", "");
    });
});
jammykam
  • 16,940
  • 2
  • 36
  • 71
0

For performance, you should add a class to the a, and targeting it directly with $('a.aClass')

Good ways to improve jQuery selector performance?

Community
  • 1
  • 1
jona303
  • 1,358
  • 1
  • 9
  • 27