views:

78

answers:

3

I have a website. It uses jQuery, a few plugins, Cufon, etc to spice things up a little, mostly visually.

The iPhone version does not need any of these files.

Can I, without resorting to subdomains, prevent the iPhone from loading these scripts?

<script src="js/jquery-1.4.min.js" type="text/javascript"></script>
<script src="js/browsersupport.js" type="text/javascript"></script>
<script src="js/jquery.easing.custom.js" type="text/javascript"></script>
<script src="js/jquery.color.custom.js" type="text/javascript"></script>
<script src="js/jquery.sound.js" type="text/javascript"></script>

<script src="fonts/cufon.js" type="text/javascript"></script>
<script src="fonts/trgo.font.js" type="text/javascript"></script>

Edit:

Using a library like this one: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

How would I go on to detect iPhone?

Browser::isBrowser('iPhone');

Returns

Fatal error: Using $this when not in object context
+2  A: 

If the user agent indicates it's an iPhone, don't load them.

Update:

According to the class you referenced, this is how it should accomplish your check - I say should simply because I can see that some users commented that this wasn't reliable for them on the blog you linked:

$browser = new Browser();
$jQuery = ($browser->getBrowser() == Browser::PLATFORM_IPHONE) ? false : true ;

See Also:

Jonathan Sampson
That is perfect. Thanks so much.
Wolfr
Glad I could help, Wolfr.
Jonathan Sampson
+1  A: 

Why do you not need them?

I am very suspicious because a number of sites try to present an "optmizied" iPhone version of a site.

Yet there is nothing I hate more than not being allowed to see the FULL site. The iPhone is a device with a real browser, please do NOT dumb-down your site so that it's almost worthless to iPhone users.

If you are calling web pages from within an iPhone app to get content, those js files would not be loaded (unless you are using a web view).

Kendall Helmstetter Gelner
A: 

Browser detection will, without any doubt whatsoever, come back to strangle you in the future. Imagine that Apple launches a new iPhone that supports all this stuff, but then you have yet more options to choose from? What about a competitor platform that pretends to be an iPhone? What about any of the billion other options that could occur..? You see the problem.

As Kendall mentions here, the better thing to do is let the user choose which site to see. I know its cool to do it automatically, but its not very maintainable, and annoying for the user. Things like language detection are different, serving a totally different version of the site is not cool for some arbitrary and very unreliable variable that could or could not be present, or could or could not be true. Look up user agent strings for IE, Firefox, Opera, Safari, and you'll start to see what's going on..

There's a pretty entertaining section in PPK's book on javascript about this, but his browser detection script might be handy if you decide to go this route (please dont!)

Browser Detection with Javascript

danp
also, look into runtime compression of assets and things like css sprites.
danp