Why does my JavaScript not execute in order?
<p>I don't understand why my code isn't running in order... The code below doesn't execute the document.write part, but it executes the part after it just fine. I think it has something to do with the timing, giving the browser to execute the . I tried using setTimeout in some parts, but it's either not working, or I'm doing it wrong.</p>

<pre><code>function isBrowserMobile()
{
    var iPadAgent = navigator.userAgent.match(/iPad/i) != null;
    var iPodAgent = navigator.userAgent.match(/iPhone/i) != null;
    var AndroidAgent = navigator.userAgent.match(/Android/i) != null;
    var webOSAgent = navigator.userAgent.match(/webOS/i) != null;
    if (iPadAgent || iPodAgent || AndroidAgent || webOSAgent)
    {
       document.write("&lt;body bgcolor='Orange'&gt;&lt;b&gt;Mobile browser detected!&lt;/b&gt;&lt;/body&gt;");
       var choice = confirm("Do you want to visit the mobile site?")
       if (choice)
           mobile();
       else
           desktop();
    }
}
</code></pre>
