Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
12-09-2011, 08:43 PM #1
- Join Date
- Dec 2011
- Thanked 0 Times in 0 Posts
Can someone help me to understand this code?
I have seen this code-piece different places - to use to make a webpage redirect if it is a mobilephone.
I don't quit understand the code - especially the line:
if (redirectagent.indexOf(redirect_devices[i]) != -1)
- why -1 ? Please help me - I would be so thankful.
Last edited by VIPStephan; 12-10-2011 at 03:20 AM. Reason: fixed code BB tag
12-10-2011, 04:13 AM #2
The indexOf method returns -1 if the text you are searching for is *NOT* found.
So by saying
!= -1that is really saying "if the given text is found anywhere in the string...".
It's kind of weird coding. If the useragent string contained, for example, "mot" at *ANY* place in the string (e.g., "Mosilla FireFox with remote scripting support") then it will count as "found".
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
12-10-2011, 07:20 AM #3
This post #5 may also help to claify things if you are still a little unsure about what indexOf does and why it returns -1why -1 ?
"The greatest revenge is to accomplish what others say you cannot do."
I used to be indecisive, but now I'm not so sure.