Pass javascript variable by jquery
<p>index.php</p>

<pre><code>&lt;script type="text/javascript"&gt;
    document.write(lastmsgID); // fail display the value (my problem is here, please help)
        function myfunction()
        {
            jQuery.get("usermessage.php, function(data) {
               document.write(lastmsgID); //success display the value
            });
        };
&lt;/script&gt;
</code></pre>

<p>usermessage.php</p>

<pre><code>&lt;?php $latestmsgid= "lovelovelove";
echo("&lt;script type='text/javascript'&gt;\n"); // pass php varible to javascript global variable lastmsgID
echo("lastmsgID = '". $latestmsgid  ."';\n");
echo("&lt;/script&gt;"); 
?&gt;
</code></pre>

<p>From my codes above, I declare global javascript variable <code>lastmsgID</code> on usermessage.php file. Then I used jQuery.get get the value of variable. Now my problem is I can only get the variable value inside myfunction() function, how can I pass the value of variable from the function to outside the function?</p>

<h3>Update:</h3>

<pre><code>&lt;script type="text/javascript"&gt;
        function myfunction()
        {
            jQuery.get("usermessage.php, function(data) {
               var test=data
               document.write(test); //success display the value
            });
        };

        document.write(test); // fail display the value because it is outside the function. (my problem is here, please help)
    &lt;/script&gt;
</code></pre>
