EchoEcho.Com KOMPLETTE WEBKURSER |
HJEM KURSER JAVASCRIPT AVANCERET |
EchoEcho.Com TOOLS & RESSOURCER |
|||||||||||||||||
| |||||||||||||||||||
|
|
|
BROWSER DETECTION SCRIPT - Step 1/3
Javascriptet som skal opsnappe browseren gør brug af navigator objektet. Dette objekt indeholder disse interessante variabler:
Det følgende var opsnappet fra din browser da du ankom til denne side: Det eneste vi behøver at gøre er at få vores side til at køre vores script når siden loades. Dette gøres simpelthen ved at skrive vores script uden funktions deklarationer. De følgende linier skal sættes ind i <head>-delen af dokumentet: BROWSER DETECTION SCRIPT - Step 2/3
Disse linier gemmer navnet på browseren i en variabel som vi kalder browsername. Hvis browseren er Microsoft Internet Explorer, gemmes "MSIE" i variablen. Hvis browseren er Netscape, gemmes "NS" i variablen. Hvis det ikke er nogen af dem , gemmes "N/A" i variablen. Now we need to find the version of the relevant browser. Since navigator.appVersion does not simply hold a value, like 2, 3 or 4, but rather would hold a text, like "3.0b4Gold (Win95; I)", we need to make a little check of the text before we can save a more convenient value in the variable called browserversion.
First we assign the value zero to the variable. If none of the checks results in assigning a value to the variable, it will still hold the zero value after the checks. A value of zero thus means that the browserversion was not available. The next 3 lines look for version numbers 2.0, 3.0 and 4.0. If navigator.appVersion contains any of the numbers, the value is stored in the variable called "browser". The complete script now looks like this: BROWSER DETECTION SCRIPT - Step 3/3
Now the script holds two variables: browsername and browserversion. This information can be used for whatever purpose we choose. The browser detection itself does nothing. All it does is - detect the visitors browser. We still haven't added if-statements that tells the browser what it should do in the different cases. In the example on the next page you will see how you can add if-statements to the browser detection script - in order to send visitors to relevant pages.
|
|
|
EchoEcho.Com KOMPLETTE WEBKURSER |
|
EchoEcho.Com TOOLS & RESSOURCER |
|||||||||||||||||||
(c) Copyright 1998-1999 NetKontoret - All Rights Reserved | |||||||||||||||||||||