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();
}
});
}
}
})
Are you sure the ajax call isn’t being fired? What happens if you add an
alert()in thesuccessfunction?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
.moreelement 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
IDfrom?P.S. This:
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
})).