Log in

View Full Version : script to detect mobile or smaller screen



BillTheBuilder
03-13-2012, 05:14 PM
I am trying to put a script into the head of my site and have it automatically detect a mobile or screen size from the browser and redirect to a mobile friendly page.

I am using


<script type="text/javascript">
if(screen.width < 600){
window.location = "http://www.sigl.ws/Phone_Page.html";
}
</script>


I built a small test site to try it out at www.sigl.ws. It is nothing fancy and not a real site. It is strictly to test this script. The main page shows a laptop and the only two buttons that work are home and phone to see what the mobile page would look like.

I cannot get it work with my android. What am I doing wrong?

Thanks

Edited----

I did try another script and it redirects great. Besides now building a smaller page to view, a new issue popped up on how to allow a mobile user view the main site without getting caught in a loop. I would also like to do this without a duplicate home page. The script I used was:



<script type="text/javascript">// <![CDATA[
var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
if (mobile) {
document.location = "http://www.mysitename.com/Phone_Page.html";
}
// ]]></script>


Thoughts?

DNSBL
03-25-2012, 09:10 AM
And maybe this might help you understand..



detectmobilebrowsers.com

BillTheBuilder
03-27-2012, 08:46 PM
Thanks for the link.

I now have the loop taken care of. I am not really versed on CSS yet and so built a small site sized for an android phone for the mobile user to land on, but not kindles or ipads and be able to get back to the main site.

EffEss
08-15-2012, 06:30 PM
BilltheBuilder, I am using the same redirect code as you but I'm having a terrible time with the loop part. Can you please post the code you used?