Question

I'm building one single page website and I want my page to load at some specific div depending if using normal computers or mobiles devices, all I have is:

<script type="text/javascript">
onload = function()
{location.href = "#home";}
</script>

The code seems to work fine but then I need to detect mobile device so page can scroll down to another div on load function, I have this code so far but I can't make it work:

<script type="text/javascript">
onload = function()
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/i)) 
{location.href = "#packages";}
</script>

How can I fix this and how can I put both codes together?

Was it helpful?

Solution

You forgot the function brackets.

<script type="text/javascript">
    onload = function() {
        if (navigator.userAgent.match(/(iPod|iPhone|iPad)/i)) {
            location.href = "#packages";
        }
    }
</script>

OTHER TIPS

Thanks to manishie i put both codes together (its probably messy but and everything is working!)

onload = function() {  
    if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android|Blackberry)/i)) {
        location.href = "#mobile";
} 
else {
location.href = "#home";
}
} //end function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top