我不明白为什么我的代码没有按顺序运行......下面的代码不执行document.write部分,但它执行后的部分就好了。我认为这与时间有关,让浏览器执行。我尝试过在某些部分使用setTimeout,但它不是工作,或者我做错了。为什么我的JavaScript不能按顺序执行?
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("<body bgcolor='Orange'><b>Mobile browser detected!</b></body>");
var choice = confirm("Do you want to visit the mobile site?")
if (choice)
mobile();
else
desktop();
}
}
这个功能有太多不好的地方。无论如何,你能链接到一个[jsFiddle](http://jsfiddle.net)来证明问题吗? – Ryan
*“它执行后的部分就好了”*:以及哪部分完全正确?如果'iPadAgent || iPodAgent || AndroidAgent || webOSAgent'为'false',则不执行'document.write'。 –
您的桌面版本通过桌面()提供?或者它是一个NOOP? – alex