Ankündigung

Einklappen
Keine Ankündigung bisher.

Hilfe bei Javascript Zufallstext

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

    Hilfe bei Javascript Zufallstext

    Hallo Elite,
    ich hoffe hier ist jemand unterwegs, der mir ggf. helfen kann. Ich möchte per Javascript einen Zufallstext in eine HTML-Seite einbinden. Dazu habe ich folgeden Code im Netz gefunden:

    Spoiler: 









    neuer Spruch


    Ich würde aber gerne die Texte in ein externes .txt-File auslagern. Allerdings habe ich keine Ahnung wie ich den Code dazu verändern muss, da ich mit Javascript absolut nichts anfangen kann.

    Vielen Dank!

    #2
    Auf welche Weise möchtest du den Zufallstext auslagern?

    Zwecks Übersichtlichkeit einfach extern? Dann würde sich eine js-Datei anbieten, welche du dann einfach mit

    Kommentar


      #3
      Selbst wenn dus in eine .txt Datei auslagerst kann man es über den Quellcode sehen, was möchtest du exakt damit bezwecken?

      Kommentar


        #4
        Ja, ausschließlich wegen der Übersichtlichkeit. Wie das Textdokument bzw. die js-Datei letztlich aussieht, ist mir egal. Ich möchte den ganzen Text nur nicht mit in das Seitendokument, weil das verdammt viel sein wird.

        Kommentar


          #5
          Dann kannst du es so splitten:

          index.html (oder wie deine Datei heißt:
          Spoiler: 






          neuer Spruch


          variablen.js
          Spoiler: 
          var z= 50;
          quotes = new Array(z);
          quotes[0] = "Nothing is as easy as it looks."
          quotes[1] = "I found out she's an angel. I don't think she knows I know. I'm worried that something might happen to me if anyone ever finds out."
          quotes[2] = "Every jumbled pile of person has a thinking part that wonder's what the part that isn't thinking isn't thing of."
          quotes[3] = "Love see's love's happiness. But happiness can't see that love is sad."
          quotes[4] = "Why the dancing, shouting, why the skrieks of pain? The lovely music, why the smell of burning autumn leaves?"
          quotes[5] = "Thank you for the card with the cartoon nurse, but there's nothing wrong with me."
          quotes[6] = "You didn't enjoy it you never believed it there won't be a refund you'll never go back."
          quotes[7] = "Lad looks at other gals. Gal thinks Jim Bean is handsomer than lad, he isn't bad."
          quotes[8] = "I don't want the world. I just want your half."
          quotes[9] = "I could never sleep my way to the top, cause my alarm clock always wakes me right up."
          quotes[10] = "Mother nature is a bitch."
          quotes[11] = "It is impossible to make anything foolproof because fools are so ingenious."
          quotes[12] = "Whenever you set out to do something, something else must be done first."
          quotes[13] = "Every solution breeds new problems."
          quotes[14] = "Trust everybody ... then cut the cards."
          quotes[15] = "Two wrongs are only the beginning."
          quotes[16] = "If at first you don't succeed, destroy all evidence that you tried."
          quotes[17] = "To succeed in politics, it is often necessary to rise above your principles."
          quotes[18] = "Exceptions prove the rule ... and wreck the budget."
          quotes[19] = "Success always occurs in private, and failure in full view."
          quotes[20] = "Buy a Pentium 586/90 so you can reboot faster."
          quotes[21] = "2 + 2 = 5 for extremely large values of 2."
          quotes[22] = "Anything that can go wrong will go wrong."
          quotes[23] = "Computers make very fast, very accurate mistakes."
          quotes[24] = "My software never has bugs. It just develops random features."
          quotes[25] = "If anything simply cannot go wrong, it will anyway."
          quotes[26] = "Best file compression around: "DEL *.*" = 100% compression"
          quotes[27] = "The Definition of an Upgrade: Take old bugs out, put new ones in."
          quotes[28] = "If everything seems to be going well, you have obviously overlooked something."
          quotes[29] = "BREAKFAST.COM Halted...Cereal Port Not Responding"
          quotes[30] = "BUFFERS=20 FILES=15 2nd down, 4th quarter, 5 yards to go!"
          quotes[31] = "It is impossible to make anything foolproof because fools are so ingenious."
          quotes[32] = "Bad command. Bad, bad command! Sit! Stay! Staaay..."
          quotes[33] = "Every solution breeds new problems."
          quotes[34] = "As a computer, I find your faith in technology amusing."
          quotes[35] = "Two wrongs are only the beginning."
          quotes[36] = "Shell to DOS... Come in DOS, do you copy? Shell to DOS..."
          quotes[37] = "All computers wait at the same speed."
          quotes[38] = "DEFINITION: Computer - A device designed to speed and automate errors."
          quotes[39] = "Success always occurs in private, and failure in full view."
          quotes[40] = "They say love is blind. I don't think you're blind."
          quotes[41] = "Everything takes longer than you think."
          quotes[42] = "Anything that can go wrong will go wrong."
          quotes[43] = "If there is a possibility of several things going wrong, the one that will cause the most damage will be the one to go wrong."
          quotes[44] = "If there is a worse time for something to go wrong, it will happen then."
          quotes[45] = "If anything simply cannot go wrong, it will anyway."
          quotes[46] = "If you perceive that there are four possible ways in which a procedure can go wrong, and circumvent these, then a fifth way, unprepared for, will promptly develop."
          quotes[47] = "Left to themselves, things tend to go from bad to worse."
          quotes[48] = "If everything seems to be going well, you have obviously overlooked something."
          quotes[49] = "Nature always sides with the hidden flaw."


          Muss hierbei aber im selben Ordner liegen. Bei Änderungen einfach Pfad bei der Einbindung ändern.

          Kommentar


            #6
            Danke

            Kommentar


              #7
              Bräuchte nochmal Hilfe:

              Ich fülle gerade die "Datenbank", sprich das "variablen.js" mit Inhalt auf. Jetzt möchte ich aber mehr als 50 verschiedene Quotes einbinden und muss dazu wohl die Werte verändern. Ich habe dazu die Variabel "var z= 50;" in der "variablen.js" auf z.b. 100 erhöht und gleichzeitig im Script selbst die Zeile "var z=(jetzt.getSeconds())%50;" ebenfalls von "50" auf meine Zahl aus der "variablen.js" angepasst. Leider kriege ich in bei den Quotes ab 49 immer "undefined" als Ausgabe...weiß jemand ne Lösung?

              Kommentar


                #8
                hast du denn quotes[50] bis quotes[99] definiert, also mit deinen Sprüchen gefüllt? wenn nicht ist das Array für diese indizes "leer"(=nicht definiert)

                Kommentar


                  #9
                  Ich hab testweise die quotes von 50 (original) auf 60 erhöht und auch entsprechend inhaltlich aufgefüllt - funktioniert leider trotzdem nicht :/

                  Kommentar


                    #10
                    Versuche es mal auf folgende Art. Vorteil: Du brauchst nicht explizit angeben, wie groß das Array sein soll. Musst das Script aber noch ein bisschen anpassen auf deine Struktur.

                    http://pastebin.com/99meeQ4z

                    Kommentar


                      #11
                      jetzt.getSeconds() wird dir wohl nur Zahlen von 0-59 zurückgeben. So kommst du nicht auf eine Höhe von 100 Indizes. Selbst mit %100 nicht. Ausserdem probier mal, den Cache zu löschen, evtl. hat der irgendwie die Variable und Arraylänge dort gespeichert.

                      lg

                      Kommentar


                        #12
                        Spoiler: 
                        // Array mit den Sprüchen
                        var sprueche = new Array();

                        // Array füllen
                        sprueche[0] = "Spruch 1";
                        sprueche[1] = "Spruch 2";
                        sprueche[2] = "Spruch 3";
                        ...

                        // Variable zufallszahl und index schaffen
                        var zufallszahl;
                        var index;

                        // Die Variable kriegt einen Wert zwischen 0 und 1
                        zufallszahl = Math.random();

                        // Wenn zufallszahl nicht 1 ist, dann wird bissl gerechnet
                        if( zufallszahl != 1.00 )
                        {
                        index = parseInt(zufallszahl * (sprueche.length + 1));
                        }
                        else
                        {
                        index = 1;
                        }

                        // Ausgabe des Spruchs
                        document.write( sprueche[index] );


                        Hab's nicht getestet. Ist einfach hier direkt reingeschrieben. Sollte eigentlich funktionieren, außer ich hab nen Denkfehler. Muss natürlich noch bissl angepasst werden. ;)

                        Kommentar

                        Lädt...
                        X