Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.post callback function calling location.reload() without using an anonymous function

Consider the following:

wrap callback in anonymous function (works)

$('#updateView').change(function(e) {
  $.post(URL + "actions/updateView.php",
    { view: $(this).val() },
    function() {reloadPage();}
  );
});

call function directly (cookie is set but doesn't seem to update before page reload)

$('#updateView').change(function(e) {
  $.post(URL + "actions/updateView.php",
    { view: $(this).val() },
    reloadPage()
  );
});

For what i am doing the first works but the second doesn't. the function reloadPage (shown below) reloads the page after updateView.php updates a cookie. For some reason using the second version the cookie isn't getting set before the page is reloading. but if i refresh the page my self, it "finds" the new value for the cookie.

is there something in the jQuery docs about this? I couldn't find anything.

function reloadPage() {location.reload(true);}

I am using jQuery 1.4.1, Php 5.2.5, Apache 2.2.11

like image 232
Samuel Avatar asked Jun 20 '26 12:06

Samuel


1 Answers

Try this:

$('#updateView').change(function(e) {
  $.post(URL + "actions/updateView.php",
    { view: $(this).val() },
    reloadPage
  );
});

reloadPage() isn't a function name, but reloadPage is :)

like image 114
Nick Craver Avatar answered Jun 23 '26 02:06

Nick Craver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!