4

So, I have this <div> in which my blog posts are contained. Its class is .post. What I want to do is that every time someone clicks on the recent posts at the widget area, the content is loaded through Ajax instead of refreshing the page.

Here is my code

jQuery(function()
{
    jQuery(".umrp-list li a").click(function(){ //here I select the links of the widget
        var post_url = jQuery(this).attr("href");
        jQuery(".post").html('<div class="loading">Loading Content...</div>');
        jQuery(".post").load(post_url);
        return false;
    });
});

So I have two problems

  1. Whenever I click on a "Recent Posts" link, the content loads where I want it to load, but my webpage is idle/frozen for about 2 seconds after the click. Why is that happening?

  2. When I click for the first time, everything works except the detail I described above. When I click for the second time (another link or even the same) then instead of loading the post I clicked, it loads my whole webpage on that .post container. Any ideas why this is happening?

2
  • 1
    What happens if you add a preventDefault() action in your click code? Change the click to: click(function(e){ Then add e.prventDefault(); within the function. Commented May 23, 2013 at 17:46
  • Thanks, but unfortunately it didn't work. Commented May 23, 2013 at 17:54

2 Answers 2

1

It sounds like you're sending the request synchronously. Synchronous operations are blocking, whereas asynchronous operations allow the UI to continue functioning while the operation completes behind the scenes.

As for the second issue, it sounds like jQuery(this) is somehow pointing to the document instead of the context of the element. Check your "this" value the second time around and it'll probably provide you with some insight.

Sign up to request clarification or add additional context in comments.

6 Comments

Hey there David. About the synchronous/asynchronous thing, I will have to check it. As for the this I actually tested it by alert(this) whenever the click is triggered and the URL is actually the correct one but it still doesn't load it as it should. The only weird thing that I saw is that when I click it for the first time, all the slashes ` of the URL are alerted as slashes whilst the second time they are alerted as %2F`
@Stevenson What is the value of jQuery(".post") the second time through in jQuery(".post").load(post_url);?
How can I check that David?
God ! You are not going to believe it! It had nothing to do with anything!!! At the single.php (it's where the blog post template is stored), I forgot to erase <?php get_header(); ?> and <?php get_footer(); ?> Now everything is great!
lol, it's so funny how the littlest things can trip you up! Glad you found it!
|
0

So the problem was that at the single.php (it's where the blog post template is stored), I forgot to erase <?php get_header(); ?> and <?php get_footer(); ?> Now everything is great!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.