...

View Full Version : Random news



Jan
01-15-2003, 07:25 PM
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. :D


~Jan

requestcode
01-15-2003, 08:00 PM
Some of the scripts here might work:
http://www.dynamicdrive.com/dynamicindex17/index.html

Also here:
http://www.javascriptkit.com/script/cutindex20.shtml

Jan
01-15-2003, 08:14 PM
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

beetle
01-15-2003, 10:14 PM
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
<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum