Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 Can someone help me to understand this code?

View previous topic View next topic Go down 
AuthorMessage
FTP EnterPrize
Member
Member
FTP EnterPrize

Posts : 1
Join date : 2011-07-05

Can someone help me to understand this code? Empty
PostSubject: Can someone help me to understand this code?   Can someone help me to understand this code? EmptySat Dec 10, 2011 4:23 am

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.
Code:

<script language=javascript>
var redirectagent = navigator.userAgent.toLowerCase();
var redirect_devices = ['vnd.wap.xhtml+xml', 'sony', 'symbian', 'nokia', 'samsung', 'mobile', 'windows ce', 'epoc', 'nitro', 'j2me', 'midp-', 'cldc-', 'netfront', 'mot', 'up.browser', 'up.link', 'audiovox', 'blackberry', 'ericsson', 'panasonic', 'philips', 'sanyo', 'sharp', 'sie-', 'portalmmm', 'blazer', 'avantgo', 'danger', 'palm', 'series60', 'palmsource',  'smartphone', 'rover', 'au-mic', 'alcatel', 'ericy', 'vodafone', 'wap1', 'wap2', 'teleca',, 'lge', 'lg-', 'iphone', 'android', 'htc', 'dream', 'webos', 'bolt', ];
for (var i in redirect_devices) {
if (redirectagent.indexOf(redirect_devices[i]) != -1) {
location.replace("http://www.example.com");
   }
}
</script>
Back to top Go down
Speak
Member
Member
Speak

Posts : 15
Join date : 2011-07-05

Can someone help me to understand this code? Empty
PostSubject: Re: Can someone help me to understand this code?   Can someone help me to understand this code? EmptySat Dec 10, 2011 4:24 am

The indexOf method returns -1 if the text you are searching for is *NOT* found.

So by saying != -1 that 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".
Back to top Go down
 

Can someone help me to understand this code?

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: Can someone help me to understand this code? Edit-trash Useless :: Trash-