screen resolution of 800x600 or greater is recommended
profile pic   ChuBlogga!
Offended? Intrigued? Contact my manager.

    Here begins your journey into the mind of everybody's favorite asian, and I don't mean Jet Li.
What follows is the somewhat inane, mostly irrelevant, and self-important ramblings of a man on the brink of madness.
Welcome... to the Chu.

Monday, August 08, 2005
 Blog hack: Random sayings    [L]

This is more of a general web page thing than an actual blogspot hack, but I figured bloggers would enjoy it. So, if you want to know how to have random messages show up on your web page, read on:
Main Idea: Display different random text show up every time a page is loaded.

NOTE: I had to put a space before all the tags ("< "), because otherwise blogspot would get all screwy in the editor - make sure to remove the spaces!

Implementation Details:
step 1 - place the random text
what this code does: define a < div> element on the webpage where you want the random text to show up.
Put this wherever you want the random text to go:
< div id="tagline">< /div>

step 2 - add necessary java code
what this code does: defines the different random texts in a javascript element, and the random selection function.
Modify the text strings to whatever you want them to say, and put this code somewhere in the < head> element:
    < script type="text/javascript" language="JavaScript">
    < !-- // TAGLINE CODE
        var taglines = new Array (
            "Random saying #1",
            "Random saying #2",
            "Random saying #3",
            "Random saying #4",
            // firefly quotes
            "You know, they tell ya to never hit a man with a closed fist - but it is, on occasion, hilarious.",
            "Also, I can kill you with my brain.",
            "I aim to misbehave.",
            "Well, my days of not taking you seriously are certainly coming to a middle." <-- notice no comma on the last line
        );
        
        function doRandomTagline()
        {
            var tagline = document.getElementById("tagline");
            if (tagline)
            {
                // get random #
                var rnd = Math.round (Math.random() * taglines.length);
                if (rnd == taglines.length) rnd--;
                // update doc
                tagline.innerHTML = taglines [rnd];
            }
        }
    // -->
    < /script>

step 3 - get the random text to appear when the page is loaded
what this code does: modifies the body element's onLoad property to run the randomizer.
Look for the < body> element somewhere right after the < /head> element, and change it to look like this:
< body onLoad="doRandomTagline();">
If you already have an onLoad method, declare a new init method that handles the current onLoad method, plus doRandomTagline(), then have the body onLoad call the new init method:

[...]
    < script type="text/javascript" language="JavaScript">
    < !-- // init
        function init()
        {
            doRandomTagline();
        }
    // -->
    < /script>
< /head>
< body onLoad="init();">

And viola, you've got random sayings. Now go annoy the heck out of people with them.



I also wouldn't call that a "hack," since, you know, it's an insanely easy script to write :-p

By Blogger Ian Dunn, at 8/08/2005 04:48:00 PM      


Well, that's really the point of the disclaimer at the top of the post:

"This is more of a general web page thing than an actual blogspot hack, but I figured bloggers would enjoy it"

And yes, it's insanely easy to write, but that doesn't mean your average blogger would know how, nor care to learn.

Therefore, the post remains relevant to its target audience, but the knowledgeable ones, such as you and I, may safely ignore it.

By Blogger ZaijiaN, at 8/08/2005 04:54:00 PM      


Grr, you did disclaim that, didn't you? In my rush to point out one of your many, many flaws I made a mistake. I really need to be more diligent when insulting people, it just looks bad when you get it wrong... Or maybe I should try that humility thing? Nah, what fun would that be...

By Blogger Ian Dunn, at 8/10/2005 03:51:00 PM      


^^^ speak up ^^^