IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Fonction de reconnaissance de browser ne marche pas


Sujet :

JavaScript

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    37
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 37
    Points : 25
    Points
    25
    Par défaut Fonction de reconnaissance de browser ne marche pas
    Bonjour à tous,

    Voici la fonction que j'ai créé et qui me permet temporairement de détecter la version du navigateur du visiteur et de le rediriger vers la page de téléchargement de la version plus récente de son browser (c'est temporaire le temps que je fasse un css compatible pour les vieux browser) :

    function detectversion() {

    var NomNav = navigator.appName;
    var VersNav = navigator.appVersion;

    if (NomNav == "Microsoft Internet Explorer" && VersNav < 7) {
    GoTo = "navweb.php";
    }
    if (NomNav == "Opera" && VersNav < 9){
    GoTo = "navweb.php";
    }
    if (NomNav == "Firefox" && VersNav < 2){
    GoTo = "navweb.php";
    }
    if (NomNav == "Safari" && VersNav < 3){
    GoTo = "navweb.php";
    }

    window.location.href = GoTo;
    }
    La partie dans ma page index.php :

    <SCRIPT LANGUAGE="Javascript" SRC="fonctionjs.js"> function detectversion(); </SCRIPT>
    Or je n'arrive pas à savoir pourquoi mais quand je teste avec un IE6 d'accèder à mon site web je ne suis pas redirigé, j'ai tout simplement la page index normale et avec des effets css réduit donc logiquement.

    Pourriez vous me corriger si possible ?

    Merci d'avance !

  2. #2
    Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 2
    Points : 2
    Points
    2
    Par défaut Détection du navigateur
    Pour plus de précision utilise le paramètre navigator.userAgent pour la recherche du navigateur !

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    function detectversion() {
     
    var VersNav = navigator.appVersion;
     
    if(navigator.userAgent.indexOf("MSIE") != -1 && VersNav < 7){
        GoTo = "/navweb.php";
    } else if(navigator.userAgent.indexOf("Firefox") != -1 && VersNav < 2){
        GoTo = "/navweb.php";
    } else if(navigator.userAgent.indexOf("Opera") != -1 && VersNav < 9){
        GoTo = "/navweb.php";
    } else if(navigator.userAgent.indexOf("Safari") != -1 && VersNav < 3){
        GoTo = "/navweb.php";
    } else if(navigator.userAgent.indexOf("Netscape") != -1){
        GoTo = "/navweb.php";
    } else {
        // Autre
        GoTo = "/navweb.php";
    }
     
    window.location.href = GoTo;
    }
    Fais appel à ta fonction dans une autre balise SCRIPT que celle avec laquelle tu appelles ton fichier "fonctionjs.js".

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    <SCRIPT LANGUAGE="Javascript" SRC="fonctionjs.js"></SCRIPT>
    <SCRIPT LANGUAGE="Javascript"><!-- 
    detectversion(); 
    // --></SCRIPT>
    Et voilà !!!

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 2
    Points : 2
    Points
    2
    Par défaut Oupsss !!!
    Desfois on parle trop vite !!!

    Le paramètre navigator.appVersion donne un chaine et non une version en clair donc tu ne peux pas utiliser ce paramètre de cette façons !

    Voici un bout de code trouver sur un autre site qui pourra t'aider :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
     
    var BrowserDetect = {
    	init: function () {
    		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    		this.version = this.searchVersion(navigator.userAgent)
    			|| this.searchVersion(navigator.appVersion)
    			|| "an unknown version";
    		this.OS = this.searchString(this.dataOS) || "an unknown OS";
    	},
    	searchString: function (data) {
    		for (var i=0;i<data.length;i++)	{
    			var dataString = data[i].string;
    			var dataProp = data[i].prop;
    			this.versionSearchString = data[i].versionSearch || data[i].identity;
    			if (dataString) {
    				if (dataString.indexOf(data[i].subString) != -1)
    					return data[i].identity;
    			}
    			else if (dataProp)
    				return data[i].identity;
    		}
    	},
    	searchVersion: function (dataString) {
    		var index = dataString.indexOf(this.versionSearchString);
    		if (index == -1) return;
    		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    	},
    	dataBrowser: [
    		{ 	string: navigator.userAgent,
    			subString: "OmniWeb",
    			versionSearch: "OmniWeb/",
    			identity: "OmniWeb"
    		},
    		{
    			string: navigator.vendor,
    			subString: "Apple",
    			identity: "Safari"
    		},
    		{
    			prop: window.opera,
    			identity: "Opera"
    		},
    		{
    			string: navigator.vendor,
    			subString: "iCab",
    			identity: "iCab"
    		},
    		{
    			string: navigator.vendor,
    			subString: "KDE",
    			identity: "Konqueror"
    		},
    		{
    			string: navigator.userAgent,
    			subString: "Firefox",
    			identity: "Firefox"
    		},
    		{
    			string: navigator.vendor,
    			subString: "Camino",
    			identity: "Camino"
    		},
    		{		// for newer Netscapes (6+)
    			string: navigator.userAgent,
    			subString: "Netscape",
    			identity: "Netscape"
    		},
    		{
    			string: navigator.userAgent,
    			subString: "MSIE",
    			identity: "Explorer",
    			versionSearch: "MSIE"
    		},
    		{
    			string: navigator.userAgent,
    			subString: "Gecko",
    			identity: "Mozilla",
    			versionSearch: "rv"
    		},
    		{ 		// for older Netscapes (4-)
    			string: navigator.userAgent,
    			subString: "Mozilla",
    			identity: "Netscape",
    			versionSearch: "Mozilla"
    		}
    	],
    	dataOS : [
    		{
    			string: navigator.platform,
    			subString: "Win",
    			identity: "Windows"
    		},
    		{
    			string: navigator.platform,
    			subString: "Mac",
    			identity: "Mac"
    		},
    		{
    			string: navigator.platform,
    			subString: "Linux",
    			identity: "Linux"
    		}
    	]
     
    };
    BrowserDetect.init();
    Voici la source : http://www.quirksmode.org/js/detect.html

    Avec ta fonction ça donnerais :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    function detectversion() {
     
    if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7){
        GoTo = "http://www.tonsiteweb.com/navweb.php";
    } else if(BrowserDetect.browser == 'Firefox' && BrowserDetect.version < 2){
        GoTo = "http://www.tonsiteweb.com/navweb.php";
    } else if(BrowserDetect.browser == 'Opera' && BrowserDetect.version < 9){
        GoTo = "http://www.tonsiteweb.com/navweb.php";
    } else if(BrowserDetect.browser == 'Safari' && BrowserDetect.version < 3){
        GoTo = "http://www.tonsiteweb.com/navweb.php";
    } else {
        // Autre
        GoTo = "http://www.tonsiteweb.com/autre.php";
    }
     
    window.location.href = GoTo;
    }
    Voilà !

  4. #4
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 637
    Points : 66 656
    Points
    66 656
    Billets dans le blog
    1
    Par défaut
    fais une recherche sur google avec ultimate browser sniffer ...
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  5. #5
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    37
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 37
    Points : 25
    Points
    25
    Par défaut
    Merci BQ911 ! Je regarde cela ce midi pour voir si ça marche!

    Par contre je ne comprend pas pourquoi le fait que j'appelle ma fonction dans la même balise que je déclare mon fichier js peut interférer dans le bon fonctionnement de celle ci ??

    Par contre avec ta deuxieme méthode donc qui devrait être la bonne, il faut aussi que je copie toute la fonction browserdetect dans le js, ça m'enchante pas des masses, c'est lourd quand même pour une simple détection.
    Pourquoi ne pas utiliser navigator.useragent alors?

    Merci

  6. #6
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    37
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 37
    Points : 25
    Points
    25
    Par défaut
    Ca marche ! Impec pour moi, merci !!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Fonction rtrim dans un foreach ne marche pas
    Par xnadyx dans le forum Langage
    Réponses: 5
    Dernier message: 14/08/2014, 12h09
  2. Réponses: 1
    Dernier message: 24/06/2014, 11h42
  3. Réponses: 9
    Dernier message: 20/09/2008, 21h56
  4. Fonction Date() sous Acces 2003 ne marche pas.
    Par fanico11 dans le forum Requêtes et SQL.
    Réponses: 11
    Dernier message: 27/05/2008, 18h22
  5. Fonction sql Round(N,n) ne marche pas ?
    Par quanou dans le forum Access
    Réponses: 1
    Dernier message: 15/05/2008, 09h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo