3

I have a link on a website that opens an iFrame in a popup box using jQuery. The jQuery script applies this function only to the link that has the specific attribute 'id=calcPop' as you can see here below.

<a href="calculator.html" id="calcPop">Click here</a>

It works great on all computers, but is very buggy on mobile devices. Is there a way to detect if a user is on a mobile device and then change that to not have an 'id' attribute?

Kara
  • 6,115
  • 16
  • 50
  • 57
Brandon
  • 33
  • 1
  • 4
  • Lots of answers already: http://stackoverflow.com/search?q=media+queries – Diodeus - James MacFarlane Apr 11 '11 at 18:10
  • Thanks for the response, I know how to target mobile devices with different CSS stylesheets but what i'm trying to do is actually edit the HTML code in the page based on whether they have a mobile device or not. I still want the user to see the link, but I don't want it to have an ID attribute if they are on a mobile device. – Brandon Apr 11 '11 at 18:17

1 Answers1

3

If you can't use a serverside language like PHP, then just remove the ID using JS – if you have jQuery, something like this will do the trick:

$("#calcPop").attr("id", "");

To detect whether you are on a mobile device is fairly involved as there are lots of mobile devices.

You could use something like:

var isMobile = navigator.userAgent.match(/Mobile/i) != null;

To find things with Mobile in the UA (that will match iPod/iPad/iPhone), not sure about others, you'd have to check.

Putting it together, in your document.ready closure:

var isMobile = navigator.userAgent.match(/Mobile/i) != null;
if (isMobile) {
    $("#calcPop").attr("id", "");
}

In PHP you could do something like:

<?php
$isMobile = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'Mobile');

if ($isMobile) {
    $id = "calcPop";
} else {
    $id = "";
}
?>

<a href="calculator.html" id="<?= $id ?>">Click here</a>
Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241
  • 1
    I tried the above code, but it didn't seem to work on my iPhone. I am able to use PHP on the site, if that will make it simpler ... I'm just not a PHP coder so not sure how to code it. – Brandon Apr 11 '11 at 19:33