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 :

Redirection lien aléatoire


Sujet :

JavaScript

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2007
    Messages : 90
    Points : 43
    Points
    43
    Par défaut Redirection lien aléatoire
    Bonjour,

    J'ai deux script l'un pour une redirection automatique:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <script type="text/javascript">
    <!--
    window.location = "http://www.annuaire-info.com/"
    //-->
    </script>
    et l'autre pour un lien aléatoire:

    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
    <SCRIPT LANGUAGE="JavaScript">
     
     
    function go_to(url) {
    window.location=url;
    }
    function rand_link() {
    var a;
    a = 1+Math.round(Math.random()*6);   // a = random number between 1-3
    if (a==1) go_to("1.php");
    if (a==2) go_to("2.php");
    if (a==3) go_to("3.php");
    if (a==4) go_to("4.php");
    if (a==5) go_to("5.php");
    if (a==6) go_to("6.php");
    }
     
    </SCRIPT>
    La fonction aléatoire est appeler comme suit:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <a href="javascript:rand_link()">
    J'aimerais que ma fonction redirection appele ma fonction aléatoire de sorte que mon lien se fasse automatiquement aléatoirement.

    Merci

    Frédéric

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    313
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 313
    Points : 330
    Points
    330
    Par défaut
    Modifie ta fonction rand_link de manière à ce qu'elle retourne une adresse aléatoire (return ...).

    Au passage, optimise : Mets tes liens dans un tableau, et tire en un par [Math.round(Math.random() * (nomtableau.length - 1))]

    Tu changes 'javascript:rand_...' par 'javascript:go_to(rand_link())'

    et enfin ton 'window.location =' du premier script devient :
    'window.location = rand_link()'

    ca te va ?

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2007
    Messages : 90
    Points : 43
    Points
    43
    Par défaut
    En fait comme je ne m'y connais pas beaucoup en javascrit je n'arrive pas è bien comprendre ce que je dois faire.

    En fait j'ai essayé ceci mais ça ne marche pas comme je le veux (la redirection automatique ne se fait pas):

    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
    <SCRIPT LANGUAGE="JavaScript">
     
    function go_to(url) {
    window.location = rand_link(); //ou comme ça window.location = rand_link;
    }
    function rand_link() {
    var a;
    a = 1+Math.round(Math.random()*6);   // a = random number between 1-3
    if (a==1) go_to("1.php");
    if (a==2) go_to("2.php");
    if (a==3) go_to("3.php");
    if (a==4) go_to("4.php");
    if (a==5) go_to("5.php");
    if (a==6) go_to("6.php");
    }
    // End -->
    </SCRIPT>
    Et j'appele ma fonction comme ça

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <a href=\"javascript:go_to(rand_link())\">
    En fait voici le contenu complet de ma page:

    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
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Document sans titre</title>
    <link rel="stylesheet" href="css/style-main.css" type="text/css" media="all" />
    <SCRIPT LANGUAGE="JavaScript">
     
    function go_to(url) {
    window.location=url;
    }
    function rand_link() {
    var a;
    a = 1+Math.round(Math.random()*6);   // a = random number between 1-3
    if (a==1) go_to("1.php");
    if (a==2) go_to("2.php");
    if (a==3) go_to("3.php");
    if (a==4) go_to("4.php");
    if (a==5) go_to("5.php");
    if (a==6) go_to("6.php");
    }
     
    </SCRIPT>
    </head>
     
    <BODY onload=startLoading()>
    <DIV ID="Main">
    <SCRIPT>
     
    var pics = new Array("images/aBox-buttons.gif",
    "photos/logo/logo-1.jpg",
    "photos/logo/logo-2.jpg",
    "photos/logo/logo-3.jpg",
    "photos/logo/logo-4.jpg",
    "photos/logo/logo-5.jpg");
     
     
    var url = "index2.php";
     
     
    var doConfirm = false;
     
     
    var canSkip = true;
     
    var imgObjs = new Array(pics.length);
    var loaded = 0;
    var total = pics.length;
    var cPercent = 0;
     
    var barLayer = null;
    var percentLayer = null;
    var doneMsgLayer = null;
     
     
    function getLayer(layerID) {
    if (document.getElementById)
    	return document.getElementById(layerID);
    else if (document.all)
    	return document.all[layerID];
    else 
    	return null;
    }
     
     
    function updateBar() {
    var percent = Math.round(loaded/total * 100);
    if (cPercent != percent)
    	{
    	cPercent = percent;
    	barLayer.style.width = (cPercent*3) +"px";
    	percentLayer.innerHTML = "<font color=\"#000000\"><B>" +cPercent+ "%</B></font>";
    	}
    if (loaded == total)
    	{
    	doneMsgLayer.innerHTML = "<a href=\"javascript:rand_link()\"><font face=\"Arial\" color=\"#000000\" size=\"2\"><B>Complété (Cliquer ici)</B></font></a>";
    	if (doConfirm && confirm("Téléchargement complété...Voulez vous continuer?"))
    		done();
    	}
    }
     
     
     
     
    function startLoading() {
    if (document.getElementById || document.all)
    	{
    	barLayer = getLayer("bar");
    	percentLayer = getLayer("percent");
    	doneMsgLayer = getLayer("doneMsg");
    	if (canSkip)
    		doneMsgLayer.innerHTML = "<a href=\"javascript:rand_link()\"><font color=\"#000000\" size=\"2\" face=\"Arial\">Annulé le préchargement</font></a>";
    	for (i=0; i<pics.length; i++)
    		{
    		imgObjs[i] = new Image();
    		imgObjs[i].onload = imgLoaded;
    		imgObjs[i].onerror = imgFailed;
    		imgObjs[i].src = pics[i];
    		}
    	}
    else
    	{
    	alert("You are likely running very old browser which is not compatible with preloading script.  Maybe it is time to update your browser.\n\nProgram is skipping preloading.");
    	window.location.replace(url);
    	}
    }
     
     
     
     
    function done() {
    parent.window.location.replace(url);
    }
     
     
     
    function imgFailed() {
    alert("The following image failed to load, probably a broken link:\n" +this.src+ "\nPlease contact the webmaster of the site you are visiting about this.  The program will skip this file now.");
    loaded++;
    updateBar();
    }
     
    function imgLoaded() {
    loaded++;
    updateBar();
    }
    </SCRIPT>
     
    <DIV id=msg style="LEFT: 100px; WIDTH: 250px; POSITION: absolute;  top: 285px;  HEIGHT: 20px"><FONT 
    face=Arial color=#000000 size=4>Préchargement en cours...</FONT></DIV>
     
    <DIV id=bg></DIV>
    <DIV id=bar></DIV>
    <DIV id=percent style="LEFT: 330px; POSITION: absolute; top: 285px;"><FONT color=#000000><B>0%</B></FONT>
    </DIV>
    <DIV id=doneMsg style="LEFT: 375px; POSITION: absolute; top: 285px;"></DIV>
    </div>
    </BODY>
     
    </html>
    Peut-être pourriez vous me donner le code complet.

    Merci

    Frédéric

  4. #4
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Points : 91 220
    Points
    91 220
    Billets dans le blog
    20
    Par défaut
    Ce n'est pas la propriété location qu'il faut modifier mais la propriété href :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    window.location.href=url;
    Et commence par virer ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <script type="text/javascript">
    <!--
    window.location = "http://www.annuaire-info.com/"
    //-->
    </script>
    Pas de question technique par MP !
    Tout le monde peut participer à developpez.com, vous avez une idée, contactez-moi !
    Mes formations video2brain : La formation complète sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'intégration de JSFiddle et CodePen sur le forum

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    90
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2007
    Messages : 90
    Points : 43
    Points
    43
    Par défaut
    Merci, je vais voir si j'y arive.

    Frédéric

Discussions similaires

  1. afficher plusieures images + liens aléatoire
    Par smed79 dans le forum Langage
    Réponses: 11
    Dernier message: 31/05/2012, 09h32
  2. Redirection lien Google
    Par timiti29 dans le forum Sécurité
    Réponses: 4
    Dernier message: 22/08/2010, 20h22
  3. redirection lien en lien2
    Par luna007 dans le forum Tomcat et TomEE
    Réponses: 1
    Dernier message: 27/04/2010, 13h08
  4. Visuel et liens aléatoires
    Par Kerweb dans le forum Langage
    Réponses: 11
    Dernier message: 01/07/2008, 14h43
  5. lien aléatoire + ouverture en popup centrée
    Par fred_station dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 13/10/2005, 10h39

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