0

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nahomy Atias
  • 193
  • 1
  • 2
  • 6

2 Answers2

1

You forgot the function brackets.

<script type="text/javascript">
    onload = function() {
        if (navigator.userAgent.match(/(iPod|iPhone|iPad)/i)) {
            location.href = "#packages";
        }
    }
</script>
manishie
  • 5,302
  • 1
  • 20
  • 21
0

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
Nahomy Atias
  • 193
  • 1
  • 2
  • 6