0

When my website first loads it runs a small script which identifies what device the user is using.

Experimenting with this i've added in a load of old devices etc... but in turn noticed the speed of the initial load has suffered on all devices (including PC's).

If I was to, for example, write this line of code at the very top of my script:

if iPhone then go to another javascript file.

Will each device still read that, go into the javascript file and read everything, thus wasting time like it already does?

Basically will I see a boost in performance if I split up my current device recogniser script?

I understand that code runs from the top down but some devices at the top still take a few seconds to settle down, longer then usual, like they are running through the whole script because it is in one big file.

3
  • Post your device sniffing script. Commented Mar 13, 2012 at 12:30
  • would be easier to answer with a code excerpt Commented Mar 13, 2012 at 12:30
  • Why aren't you reading the User-Agent header and just including the correct script(s) necessary at that point? (That's what most "mobile detection" scripts do. Commented Mar 13, 2012 at 12:31

4 Answers 4

1

If you load a script, it will always be parsed before it's executed. So putting a condition in a script will only keep the code from actually being executed, it's still loaded and parsed every time.

If you want to keep unwanted scripts from being loaded and parsed, you would need one script that conditionally loaded another script.

Sign up to request clarification or add additional context in comments.

Comments

0

I would dynamically load the extra JS like so:

var isIphone = (function(){ /*determin if is iPhone, return true or false*/})();

if (isIphone){ 
  var iPhone_js = document.createElement('script')
  iPhone_js.setAttribute("type","text/javascript")
  iPhone_js.setAttribute("src","/root/to/iphone/js")
 }

Comments

0

You can check what device the client is using by looking at the User Agent string:
http://en.wikipedia.org/wiki/User_agent

Then, you can just return different HTML(that includes different scripts) for each device.

Comments

0

if you are using server side language in your website, I think it is best then, to decide on the server side, what is the target device (and remember it for subsequent requests) then send the javascript library or files accordingly to client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.