1

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.

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();
    }
}
5
  • 3
    There is so much bad in this function. Anyway, can you link to a jsFiddle that demonstrates the problem, please? Commented Jun 20, 2012 at 2:49
  • 3
    "it executes the part after it just fine": and which part is that exactly? If iPadAgent || iPodAgent || AndroidAgent || webOSAgent is false, then document.write is not executed. Commented Jun 20, 2012 at 2:51
  • Your desktop version is rendered via desktop()? Or is it a NOOP? Commented Jun 20, 2012 at 2:56
  • Both mobile() and desktop() calls other functions not listed here. What I mean is that document.write just doesn't execute at all, it just continues to the if...else... part. By the way, I was testing this on my iPod touch, so it should have executed the document.write part. Commented Jun 20, 2012 at 2:58
  • I would try replacing the body tag with div. Commented Jun 20, 2012 at 3:00

2 Answers 2

1

EDIT:

I suggest you not to use document.write but to use DOM, like the following:

document.body.style.backgroundColor='Orange';
document.body.innerHTML='This is a text';

And when testing your code in a desktop browser you can add true || to the start of the if condition.

My original answer used innerText which is not a standard

Sign up to request clarification or add additional context in comments.

Comments

0

Browsers rarely update the page before a function has finished processing. So the document.write has been executed, but the result may not be displayed until the function is complete.

As minitech said, it's a pretty rubbish function anyway. You should be asking based on screen size, not UA sniffing.

Note that if the function is run after the document has finished loading, the call to document.write will wipe out the entire contents of the document, including the script.

10 Comments

"Browsers rarely update the page before a function has finished processing" Where did you get that from?
That's how they work. The browser doesn't know when script has finished modifying a page, so rather than display changes as they happen (which would be annoying), they wait util all changes have been made then show a single update. document.write is a little different, some browsers will display the results immediately but others may wait.
I don't know Rob, a simple test seems to support @valentinas observation. Can you provide a counter-demonstration or something in the specification?
Have you tried it in an iPad? In IE 8 half the time the second prompt appears before the page is rendered. And on my iPhone, the word "test" doesn't appear until after the second prompt is dismissed.
Initial impression: You have an iPad with IE8 on it? Let me check it out... on Win7. I tested on Chrome.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.