Kali, since we're wondering if this is a jQuery problem, let's find out by
taking jQuery out of the equation. Where the code uses jQuery to load the
message into the #mailSuccess element, we'll just alert it instead - but
we'll call alert in exactly the same way that you're calling jQuery now.

First, I assume there's a typo or two in this code:

  function successHome(message) {
    setTimeout('$("#mailSuccess").html(message) 1000);

and it actually reads:

  function successHome(message) {
    setTimeout('$("#mailSuccess").html(message)', 1000);
  }

Correct?

So let's change it to:

  function successHome(message) {
    setTimeout('alert(message)', 1000);
  }

That doesn't work either, does it? This helps narrow it down: it's not a
jQuery problem, so it must be a problem in this little bit of code itself.

What's wrong? When you call setTimeout with a text string, that code gets
executed *in the global scope*, i.e. just like code that is outside of any
function at all. Your message variable is local to the successHome function,
so code running in the global scope can't see it.

Instead of passing a string to setTimeout, it's almost always better to pass
a function. An inline anonymous function is often used:

  function successHome(message) {
    setTimeout( function() {
      alert(message);
    }, 1000 );
  }

Now the code will work as you expect, because the inner function has access
to the variables of the outer function.

Going back to the jQuery code, it would be:

  function successHome(message) {
    setTimeout( function() {
      $("#mailSuccess").html(message);
    }, 1000 );
  }

-Mike

> From: kali
> 
> I solved this, BUT:
> 
> another weird problem:
> 
> var message is passed to the function, so I have:
> 
> function successHome(message) {
>       setTimeout('$("#mailSuccess").html(message) 1000);
> 
> but it says 'message' is not defined
> 
> only this works:
> 
> function successHome(message) {
>       setTimeout('$("#mailSuccess").html("Your email has been 
> sent.<p>Thank you.</p>");', 1000);
> 
>  this is so bizarre....
> 
> if I do:
> 
> function successHome(message) {
>       alert(message);    //  no probl here
>     //        but here:
>       setTimeout('$("#mailSuccess").html(message);', 1000);
>      //   it says 'message is not defined'
> 
> 
> ???????
> 
> thank you....
> 

Reply via email to