Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide div in smartphone or mobile browser

i want to hide my div if someone visit from smartphone, mobile etc. my javascript code not work for me, please let me know how to fix it?

<html>
<head>
<title>Test</title>
<script type="text/javascript">
    if (/Android|webOS|iPhone|iPad|iPod|pocket|psp|kindle|avantgo|blazer|midori|Tablet|Palm|maemo|plucker|phone|BlackBerry|symbian|IEMobile|mobile|ZuneWP7|Windows Phone|Opera Mini/i.test(navigator.userAgent)){
     document.getElementById('mybox').style.display = 'none';
     };
</script>
</head>
<body>

<div id="mybox">
Hello world
</div>

</body>
</html>
like image 354
Ahmed iqbal Avatar asked Mar 19 '14 11:03

Ahmed iqbal


2 Answers

You need to use media query to create responsive UI,
For different screen resolution you should have different CSS for all your HTML component(Could be Div/CSS Class etc.)
Search for some good responsive tutorial you'll surely find that interesting
Now a days people using Twitter Bootstrap to make the UI responsive, It has many responsive classes those are more useful to create Rapid responsive UI development. Sample

@media screen and (max-width: 960px) {

}
like image 115
Jaykishan Avatar answered Sep 24 '22 02:09

Jaykishan


You are missing the window.onload

<html>
<head>
<title>Test</title>
<script type="text/javascript">
    if (/Android|webOS|iPhone|iPad|iPod|pocket|psp|kindle|avantgo|blazer|midori|Tablet|Palm|maemo|plucker|phone|BlackBerry|symbian|IEMobile|mobile|ZuneWP7|Windows Phone|Opera Mini/i.test(navigator.userAgent)){
     window.onload = function (){
         document.getElementById('mybox').style.display = 'none';
     }
   };
</script>
</head>
<body>

<div id="mybox">
Hello world
</div>

</body>
</html>

But the css approach seems cleaner.

like image 42
helder.tavares.silva Avatar answered Sep 24 '22 02:09

helder.tavares.silva