The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Ipad sniffer
Discuss Ipad sniffer in the JavaScript Development forum on Dev Shed. Ipad sniffer JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 28th, 2012, 09:12 AM
|
|
|
|
Ipad sniffer
OK, I have JS for the Ipad sniffer which looks like this
Code:
<script language=”javascript”>
if (navigator.userAgent && ( navigator.userAgent.indexOf(“iPad”) > -1 || navigator.userAgent.indexOf(“iPhone”) > -1 ))
{
window.location.replace(“http://www.YOUR-URL-GOES-HERE.com/iPhone-iPad.html”);
}
</script>
But instead of redirecting, I want to include a different css file.
How do I do this with JavaScript?
thanks 
|

November 28th, 2012, 01:55 PM
|
 |
Contributing User
|
|
|
|
Code:
<script>
function iPad_Alt_CSS()
{
var oldCSS = document.getElementsByTagName("link")[0];
var newCSS = document.createElement("link");
newCSS.setAttribute("rel","stylesheet");
newCSS.setAttribute("type","text/css");
newCSS.setAttribute("href","iPad.css");
if (navigator.userAgent && ( navigator.userAgent.indexOf(“iPad”) > -1 || navigator.userAgent.indexOf(“iPhone”) > -1 ))
{
document.getElementsByTagName("head")[0].removeChild(oldCSS);
document.getElementsByTagName("head")[0].appendChild(newCSS);
}
}
document.onreadystatechange = function() {
new iPad_Alt_CSS();
}
</script>
|

November 28th, 2012, 02:30 PM
|
|
|
|
thanks web__loone08
But I just need to include an extra css file in the header for Ipads. My code should look something like this::
Code:
<script language=”javascript”>
if (navigator.userAgent && ( navigator.userAgent.indexOf(“iPad”) > -1 || navigator.userAgent.indexOf(“iPhone”) > -1 ))
{
// include extra css here. for example, css/ipad.css
}
</script>
thanks for your help! 
|

November 28th, 2012, 02:44 PM
|
 |
Contributing User
|
|
|
|
Then... this will just add another link tag for your iPad.css file; instead of removing your existing CSS link tag... like I originally did.
Code:
<script>
function iPad_Alt_CSS()
{
var newCSS = document.createElement("link");
newCSS.setAttribute("rel","stylesheet");
newCSS.setAttribute("type","text/css");
newCSS.setAttribute("href","iPad.css");
if (navigator.userAgent && ( navigator.userAgent.indexOf(“iPad”) > -1 || navigator.userAgent.indexOf(“iPhone”) > -1 ))
{
document.getElementsByTagName("head")[0].appendChild(newCSS);
}
}
document.onreadystatechange = function() {
new iPad_Alt_CSS();
}
</script>
Are you wanting the "style" tag (internal css) added to the head of the document; instead of using a "link" tag (for external css file)?
If so... you could do something like this:
Code:
<script>
var iPad_CSS = "\nbody {\nbackground:blue\n}\n"; // add your style to this variable
function iPad_Alt_CSS()
{
var newCSS = document.createElement("style");
var defineCSS = document.createTextNode(iPad_CSS);
newCSS.setAttribute("type","text/css");
newCSS.setAttribute("media","screen");
if (navigator.userAgent && ( navigator.userAgent.indexOf(“iPad”) > -1 || navigator.userAgent.indexOf(“iPhone”) > -1 ))
{
document.getElementsByTagName("head")[0].appendChild(newCSS);
document.getElementsByTagName("style")[0].appendChild(defineCSS);
}
}
document.onreadystatechange = function() {
if (document.readyState=="interactive") {
new iPad_Alt_CSS();
}
}
</script>
Last edited by web_loone08 : November 28th, 2012 at 03:34 PM.
|

November 28th, 2012, 03:38 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
Is there a particular reason why you need to use JavaScript for this? CSS3 Media Queries were designed for this kind of thing. Target iPhone and iPad with CSS3 Media Queries
@web_loone08: Do you know which browsers (besides IE, which introduced it long ago) support the "onreadystatechange" event on the "document"?
|

November 28th, 2012, 03:45 PM
|
 |
Contributing User
|
|
|
|
|
@Kravvitz: IE, FF, & Chrome... all support the "document.onreadystatechange" event. I cannot say that I have tested it in other OS platforms; but these are three of the most commonly used OS platforms. I would assume Apple supports this event in Safari browsers, as well. Also... I thought about suggesting media queries, as an answer to this question; I just thought the OP wanted something like this for a specific reason. However, CSS3 Media Queries are not supported by the majority of older browser versions; hence a JavaScript workaround is sometimes required to replicate a media query affect.
Last edited by web_loone08 : November 28th, 2012 at 05:27 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|