문제

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?

도움이 되었습니까?

해결책

You forgot the function brackets.

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top