EchoEcho.Com
KOMPLETTE WEBKURSER

HJEM KURSER JAVASCRIPT AVANCERET     
EchoEcho.Com
TOOLS & RESSOURCER
 HJEM 
 
 KURSER 
 
 RESSOURCER 
 
 REDSKABER 
 
 DOKUMENTATION 
   
 OVERSIGT 
  
  SØG PÅ DETTE SITE

FORESLÅ DETTE SITE
FORESLÅ DETTE SITE      

BROWSER DETECTION

INTRODUKTION


TEKNIKKEN


KODEN


EKSEMPEL


DETECT REDSKAB

Browser Detection :
KODEN

Først skal du lave en standardside som du laver til alle HTML-sider:

BROWSER DETECTION SCRIPT - Step 1/3
<html>
<head>
<title>Browserdetection</Title>
</head>

<body>
</body>
</html>


Javascriptet som skal opsnappe browseren gør brug af navigator objektet.

Dette objekt indeholder disse interessante variabler:

VARIABLERBESKRIVELSE
navigator.appCodeName
Kodenavnet på browseren
(f.eks. Mozilla)
navigator.appName Navnet på browseren
(f.eks. Netscape eller Microsoft Internet Explorer)
navigator.appVersion Browser version (f.eks. 3.0 or 4.0)
navigator.userAgent Header information for browseren.
(e.g. Mozilla/4.0)
navigator.platform Brugerens operativ system (f.eks. WIN32)


Det følgende var opsnappet fra din browser da du ankom til denne side:

navigator.appCodeName
navigator.appName
navigator.appVersion
navigator.userAgent
navigator.platform


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

<html>
<head>
<title>Browser detection</Title>
<Script Language="JavaScript">
browsername=navigator.appName;
if (browsername.indexOf("Netscape")!=-1) {browsername="NS"}
else
{if (browsername.indexOf("Microsoft")!=-1) {browsername="MSIE"}
else {browsername="N/A"}};
</script>

</head>

<body>
</body>
</html>


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.

browser="0";
if (navigator.appVersion.indexOf("2.0")!=-1) {browser="2"};
if (navigator.appVersion.indexOf("3.0")!=-1) {browser="3"};
if (navigator.appVersion.indexOf("4.0")!=-1) {browser="4"};


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

<html>
<head>
<title>Browser detection</Title>
<Script Language="JavaScript">
browsername=navigator.appName;
if (browsername.indexOf("Netscape")!=-1) {browsername="NS"}
else
{if (browsername.indexOf("Microsoft")!=-1) {browsername="MSIE"}
else {browsername="N/A"}};
browser="0";
if (navigator.appVersion.indexOf("2.0")!=-1) {browser="2"};
if (navigator.appVersion.indexOf("3.0")!=-1) {browser="3"};
if (navigator.appVersion.indexOf("4.0")!=-1) {browser="4"};

</script>
</head>

<body>
</body>
</html>



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.



<< TILBAGE
LÆS MERE >>

JAVASCRIPT MENU

JAVASCRIPT INTRO


ANIMEREDE KNAPPER


BROWSER DETECTION


COOKIES


POPUP VINDUER


DROP DOWN MENU


FORM VALIDERING


FRAMESET SCRIPT


MULTI LINK








EchoEcho.Com
KOMPLETTE WEBKURSER
 HJEM 
 
 KURSER 
 
 RESSOURCER 
 
 REDSKABER 
 
 DOKUMENTATION 
   
 OVERSIGT 
EchoEcho.Com
TOOLS & RESSOURCER

(c) Copyright 1998-1999 NetKontoret - All Rights Reserved