Kyle Kyle - 1 month ago 6
Java Question

Java Servlets - How do I detect if a user is from a mobile device?

Java Servlets - How do I detect if a user is from a mobile device?

I'm using the TinyMCE javascript editor, and it doesn't work on the iphone. How can I detect if the user is coming from a mobile device?

Answer

I'm using the TinyMCE javascript editor

Since you'd like to change the client side behaviour depending on the client, best is to handle this at the client side rather than the server side.

In CSS world, you can hook on the media type to apply styles depending on the media used. Most used media types are screen (usually PCs), handheld (usually mobiles) and print (for the printed page).

You can make use of it to hide the editor by just the following rule in your CSS:

@media handheld {
    #elementIdContainingEditor { display: none; }
}

You can even specify separate stylesheets depending on the media used.

<link rel="stylesheet" href="default.css" media="screen">
<link rel="stylesheet" href="mobile.css" media="handheld">

If the problem is actually more that it doesn't work because JavaScript is disabled on the particular client, then you better have to execute the particular CSS when JS is disabled:

<noscript><style>#elementIdContainingEditor { display: none; }</style></noscript>

Or the other way round, initially hide it and then show it when JS is enabled:

<script>document.getElementById('elementIdContainingEditor').style.display = 'block';</script>

This is more reliable than sniffing the agent in the server side.

Comments