LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2006, 11:26 AM   #1
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Rep: Reputation: 62
Incorporate 'fortune' into HTML page


I use a local HTML file as my browser home page. It started out as the localstart.html file provided by Mozilla, but I've added various personal pictures, text, etc. Now I want to try something more complicated, but I can't seem to get a handle on where to start.

I'd like to have a little box on the page that contains the output of 'fortune.' I think it may involve javascript, but none of the javascript examples I can find actually executes a program. I don't want to click any buttons or anything like that ... just have a newly generated 'fortune' every time I open the file.

Is that possible? ... and what HTML functions should I be studying?

Last edited by rickh; 06-29-2006 at 11:30 AM.
 
Old 06-29-2006, 12:24 PM   #2
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
A quickNnotTooDirty solution adapted and cleaned up from this
(by the way... I found this page typing "javascript fortune" in google...)

In the head of your document add the following script
Code:
    <script type="text/javascript">
      function howLuckyAmIToday () {

        var fortuneBox, fortunes, index, quoteText, quote;

        /* Get the box where we should display the fortune */
        fortuneBox = document.getElementById('fortuneBox');

        /* Feed the fortunes array, add more on the same template if needed */
        fortunes = new Array();
        fortunes[fortunes.length] = "Our future is whatever you make it, so make it a good one...";
        fortunes[fortunes.length] = "You will meet a strange person.";
        fortunes[fortunes.length] = "Luck is on your side today";
        fortunes[fortunes.length] = "Do NOT go in there";
        fortunes[fortunes.length] = "Whatch your back";
        fortunes[fortunes.length] = "You will break your leg";

        /* Calculate a random index */
        index = Math.floor(Math.random() * fortunes.length);

        /* Create a quote element, feed the random quote in
         * and add the quote to fortune box
         */
         quoteText = document.createTextNode(fortunes[index]);
         quote = document.createElement('q');
         quote.appendChild(quoteText);
         fortuneBox.appendChild(quote);
      }
    </script>
This step registers a javascript function that will feed a quoted fortune inside an html element having the id "fortuneBox". So the next step is to make this box on your page. At the place you want your fortune to appear:
Code:
<div id="fortuneBox"></div>
You can replace the element by 'p' or whatever box element. The important thing here is the id attribute

Last, you need to fire the function when the page loads. So modify the body element of your page like this:
Code:
<body onload="howLuckyAmIToday()">
That's it ! Now you just need some CSS rules to style your fortune as desired.

This solution will be OK for a reasonable number of fortunes. For a much larger number of fortunes, another solution might be preferable: server side script + textfile, server side script + database.... all of this being interpreted directly by your page or accessed by javascript.
 
Old 06-29-2006, 12:43 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...and if you would want to do it w/o Javascript (kinda cool for browsers that don't or can't use it), and if you serve files already (any tiny cgi-capable httpd would do) and your webserver user is allowed to execute fortune (or through sudo) then a very small CGI script could do. Lame shellscript example:
Code:
#!/bin/sh - 
echo "Content-type: text/html"; echo ""
echo "<html><head><title>Fortune</title></head><body><pre>"
# insert fortune command and args here
echo "</pre></body></html>"; exit 0
 
Old 06-29-2006, 12:43 PM   #4
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Original Poster
Rep: Reputation: 62
Quote:
I found this page typing "javascript fortune" in google...
Arrgh! Oh well, thanks for doing it, and I'm sure that gives me enough to get it rolling.
 
Old 06-29-2006, 01:13 PM   #5
rickh
Senior Member
 
Registered: May 2004
Location: Albuquerque, NM USA
Distribution: Debian-Lenny/Sid 32/64 Desktop: Generic AMD64-EVGA 680i Laptop: Generic Intel SIS-AC97
Posts: 4,250

Original Poster
Rep: Reputation: 62
Hmmm! After looking the script over more closely, I can't really use the embedded data. This page is only accessed by my family and me, and we want to be surprised ... much like the real 'fortune' program.

So... it appears that I'm going to have to set up a little private http server whose only function is to serve the 'home page' of my internet browser with the output of $ fortune (as well as other things I eventually think of). More complicated than I originally perceived, but more interesting as well.

Quote:
...any tiny cgi-capable httpd would do...
I'm off to find one.
 
Old 06-30-2006, 12:01 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It wouldn't surprise me if there is a publicly accessible version of such a service somewhere; kind of like the public hit counters that can be found. Then you would just have to put the appropriate link on your page.

A bit of creative browsing/searching may turn one up.

--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
html-page timestamp? linmix Linux - General 3 12-25-2005 04:39 PM
html page maker prabhakar_kushwaha Linux - Software 3 10-14-2005 09:17 PM
HTML page break? patpawlowski Programming 4 02-12-2004 10:30 AM
Creating HTML page with link in C jorgedf Programming 3 08-01-2003 02:47 AM
Apache: I want .pl as default page not html jkahlich Linux - Networking 2 10-29-2001 10:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration