Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Random news

  1. #1
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random news

    Hey all!

    I was wondering, if there's an easy way to put topics of some news on top of each page, and everytime the page is reloaded or a new page is opened, it will change. The content (or should i say the topics + links) should be located in an external javascript file so that it's easy to update it while affecting all pages. All help is greatly appreciated.

    Thanks so much.


    ~Jan

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #3
    Jan
    Jan is offline
    Regular Coder
    Join Date
    Jul 2002
    Location
    Finland
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Been there, done that

    Thanks for your advice, but there aren't any scripts there tha fit my needs. I was looking for a simple script that changes the topic every time the page reloads or a new page is opened.

    Thankies anyhow


    ~Jan

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I'm not sure how much data per item you'd like to store, but here's a quickie example using just a Headline and a Body. These are some methods and function I took from a DataList class I have already written. I'm including only what is pertinent to what you need. As you can see, there is a couple different ways to add data to the list. Event the output could be consolidated into a method, but I wasn't sure what you'd want. This example also shows you can add the data to the DataList with the constructor function as well. I did it here as a 2nd list so you could see the difference
    Code:
    <html>
    <head>
    
    <script language="JavaScript">
    
    // Constructor
    function DataList( listName, data )
    {
    	this.listName = listName;
    	this.dataNames = new Array( "Headline", "Body" );
    	this.propNames = new Array( "headline", "body" );
    	this.quantity = 0;
    	this.list = new Array();
    	if ( typeof data != 'undefined' )
    		this.addDataItems( data );
    }
    
    // Constructor
    function DataItem( headline, body )
    {
    	this.headline = headline;
    	this.body = body;
    }
    
    // Alternate constructor
    function DataItemInternal( props )
    {
    	var arg1 = arguments[1];
    	for ( var i = 0; ( prop = props[i] ); i++ )
    	{
    		this[prop] = arguments[1][i];
    	}
    }
    
    // Uses alternate constructor
    DataList.prototype.addDataItem = function()
    {
    	this.list[this.list.length] = new DataItemInternal( this.propNames, arguments );
    	this.quantity++;
    }
    
    // Uses alternate constructor, takes 2d array as argument
    DataList.prototype.addDataItems = function( arr )
    {
    	for ( var i = 0; ( itm = arr[i] ); i++ )
    	{
    		this.list[this.list.length] = new DataItemInternal( this.propNames, itm )
    		this.quantity++;
    	}
    }
    
    // returns a random data item from the list
    DataList.prototype.getRandom = function()
    {
    	var rand = Math.floor( Math.random() * this.quantity );
    	return this.list[rand];
    }
    
    var d = new DataList( 'Topics' );
    d.addDataItem( 'My First Item!', 'Here is a sample of how this stuff can be done' );
    d.addDataItem( 'My 2nd item', 'I\'m not very good at making this stuff up' );
    d.addDataItem( 'Woohoo!', 'Don\'t ask why, I\'m just trying to fill up space here' );
    d.addDataItem( 'Number four', 'Just because I felt like I needed four sample pieces of data.' );
    
    var data = [
    	['Array 1','This data came from the array'],
    	['Array 2','This data came from the array also'],
    	['Array 3','This data came from the array too!']
    	];
    d.addDataItems( data );
    
    var d2 = new DataList( 'Topics2', data );
    
    </script>
    
    </head>
    
    <body>
    
    <script type="text/javascript">
    
    var topic = d.getRandom();
    document.write( "<b>" + topic.headline + "</b>" );
    document.write( "<p>" + topic.body + "</p>" );
    
    document.write( "<hr />" );
    
    topic = d2.getRandom();
    document.write( "<b>" + topic.headline + "</b>" );
    document.write( "<p>" + topic.body + "</p>" );
    
    </script>
    
    </body>
    </html>
    Last edited by beetle; 01-15-2003 at 10:41 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •