views:

841

answers:

1

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

+3  A: 

Try this:

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

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

Nick Craver
it works! Is that a jQuery thing or is that just Javascript? if jQuery, is there some docs that show this? On the doc page for $.post it doesn't have an example without using an anonymous function.
Samuel
@Samuel - It's javascript in general, or most languages even. For example in c#: `MyClass.Event += MyFunc` works, but `MyClass.Event += MyFunc()` doesn't...since you don't know the number of params, the languages are written to take the function name without params or parenthesis.
Nick Craver
@Samuel - Remember that in JavaScript everything is an object and so are function. So if you say reloadPage you reference the object, if you say reloadPage() you are executing. Here's an example where the function is declared, than stored in a variable and then executed through that variable: `function hello() { alert('hello'); }; var x = hello; x();`
SBUJOLD