Solved

java script if clause

Posted on 2012-04-08
6
252 Views
Last Modified: 2012-06-27
I have line of information I would like to leave out if the site visitor is looking at a site via an ipad. Is there any java script that will allow me to do this:

if !ipad "some string"

Thanks,
N
0
Comment
Question by:Neil_Bradley
  • 4
  • 2
6 Comments
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37822008
if(!(/iPad/.test(navigator.userAgent)))
{
//do stuff
}

Open in new window

0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37822015
to make it slightly more robust you could make the regex case insensitive:

/iPad/i instead of /iPad/
0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 37822051
Thanks basicinstinct,
if I wanted to echo an alternate style sheet using your code how would I do this?
IE
if(!(/iPad/.test(navigator.userAgent)))
{
<link rel="stylesheet" href="css/style.css">
}
Cheers,
N
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 23

Accepted Solution

by:
basicinstinct earned 300 total points
ID: 37822129
well normally that would be done on the server side,
but for argument's sake if you want to do it on the client side then you could do something like this:

(function(){
if(!(/iPad/i.test(navigator.userAgent)))
{
    var newCss = document.createElement("link");
    newCss.setAttribute("rel", "stylesheet");
    newCss.setAttribute("href", "css/style.css");
    document.body.appendChild(newCss);
}
})();¿
0
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 37822157
That's perfect and extremely thorough, thank you.
I am guessing that an alternate method code be:
 if(/iPad/.test(navigator.userAgent))
{
document.write("<link rel=\"stylesheet\" href=\"css/device.css\">");
}
        </script>

Cheers,
N
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 37831440
yes but i NEVER use document.write, it makes me do this

>:(
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
migrating to phpbb forum from vBulletin 4.2 3 23
PHP: Filling Out/Creating a PDF 29 94
Sending ASP to server side 8 28
Rotate images for Web Page 11 24
This article will show, step by step, how to integrate R code into a R Sweave document
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question