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>
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>
Share
Improve this question
asked Mar 19, 2014 at 11:37
Ahmed iqbalAhmed iqbal
5971 gold badge8 silver badges29 bronze badges
3
-
you can use CSS
@media queries
to hide any div. By using media query you can target the any device. – Kheema Pandey Commented Mar 19, 2014 at 11:41 - CSS media Query is the answer for your question. Read article to know more mobile.smashingmagazine./2013/03/05/… – Harish Boke Commented Mar 19, 2014 at 11:43
- The problem is (I think so) that the code is executed before your html div element has been renderized, if you move the code to the end of your html file It should work. – Roberto Commented Mar 19, 2014 at 11:44
7 Answers
Reset to default 3You need to use media query to create responsive UI,
For different screen resolution you should have different CSS
for all your HTML ponent(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) {
}
Use media query to hide
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
#mybox{
display: none
}
This will hide when screen size below 480px
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.
Remove semicolon of if condition close block:
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';
}//semicolon is removed
You can hide or show someone on any screen by media queries.
@media (min-width: 768px) and (max-width: 979px) {
.div{
display : block; For big screen
}
}
@media (min-width: 380px) and (max-width: 420px) {
.div{
display : none; For small screen
}
}
hello my friend try this..
/* Smartphones (portrait and landscape) ----------- */
@media only screen and (min-device-width : 320px) and (max-device-width : 480px)
{
#mybox
{
display: none;
}
}
/* Smartphones (landscape) ----------- */
@media only screen and (min-width : 321px)
{
#mybox
{
display: none;
}
}
/* Smartphones (portrait) ----------- */
@media only screen and (max-width : 320px)
{
#mybox
{
display: none;
}
}
/* iPads (portrait and landscape) ----------- */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px)
{
#mybox
{
display: none;
}
}
/* iPads (landscape) ----------- */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape)
{
#mybox
{
display: none;
}
}
/* iPads (portrait) ----------- */
@media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait)
{
#mybox
{
display: none;
}
}
/* Desktops and laptops ----------- */
@media only screen and (min-width : 1224px)
{
#mybox
{
display: block;
}
}
/* Large screens ----------- */
@media only screen and (min-width : 1824px)
{
#mybox
{
display: block;
}
}
/* iPhone 4 ----------- */
@media only screen and (-webkit-min-device-pixel-ratio : 1.5),only screen and (min-device-pixel-ratio : 1.5)
{
#mybox
{
display: none;
}
}
Put your JS code at the end of the page or use an onLoad
event.
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="mybox">
Hello world
</div>
<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>
</body>
</html>