tags:

views:

39

answers:

1

Hi,

How would I make an ajax call if a page detects a cookie is present? The code below fires the alert but not the ajax call. Any help greatly appreciated.

EDIT: Updated and fully working code below. Simply had to change $(this) to $('.more') when the cookie is found. Many thanks for help/advice.

$(document).ready(function(){                                                     
        //More Button            
        $('.more').live("click",function() 
        {    
        $.cookie('viewing_expanded_content',true, { expires : new Date(new Date().valueOf() + 60 * 60 * 1000) });
        var ID = $(this).attr("id");
        if(ID)
        {                        
        $("#more"+ID).html('<img src="images/more_press.gif" alt="More results..." />');
        $.ajax({                   
        type: "POST",
        url: "more_press.php",
        data: "lastmsg="+ ID, 
        cache: true,
        success: function(html){                                        
        $("div#updates").append(html);
        $("#more"+ID).remove();
                }            
            });
        } else {
        $(".morebox").html('<p><strong>No more results...</strong></p>');
        }
        return false;
                });

        var viewing_expanded_content = $.cookie('viewing_expanded_content');
        if ( viewing_expanded_content == 'true' ) {

        //alert("Active cookies!");    

        var ID = $('.more').attr("id");
        if(ID)
        {                        
        $("#more"+ID).html('<img src="images/more_press.gif" alt="More results..." />');
        $.ajax({                   
        type: "POST",
        url: "more_press.php",
        data: "lastmsg="+ ID, 
        cache: true,
        success: function(html){                                        
        $("div#updates").append(html);
        $("#more"+ID).remove();
                }            
            });
        }

            }

        })            
A: 

Are you sure the ajax call isn't being fired? What happens if you add an alert() in the success function?

Update: I'm working through your referenced code, but it's difficult to tell what you're trying to do. You're setting a cookie and making an ajax call when a .more element is clicked. You then have some code that checks the cookie, and makes the ajax call if that cookie is set.

In what circumstances should that second bit of code run? Should it happen on page load? If so, where should it get the value of ID from?

P.S. This:

$(document).ready(function(){                                                 
    $(function() {

doesn't make sense. The second line is a shortcut for the first, so having the two is redundant; just get rid of that first line (and its matching })).

Bobby Jack
Better to put this as a comment.
rahul
rahul - fair point, although this kind of troubleshooting question can very easily end up as a whole bunch of comments and no answers. If there's no productive 'final answer' I can produce by editing this one, I'll remove it.
Bobby Jack
nothing happens when alert() added to success function.
ss888
here is a link to my previous question with the full code - http://stackoverflow.com/questions/3923003/jquery-ajax-cookie-pt2
ss888
There are many problems with that referenced code. I'll post a cleaned up version, if you want; in short, though, ID is NOT being set at this point. It's difficult to tell because the code is inconsistently formatted - if you clean up your tabs, it should become obvious.
Bobby Jack
ok, many thanks.
ss888