Centering a Java Script

  • Thread starter Thread starter Pictures
  • Start date Start date
Pictures said:
How do I center a Java Script on a page?

<center><script type="text/javascript"><!--
....
--></script></center>

Or you can use CSS to get the job done. How's your javascript
code look?
 
<script type="text/javascript">

/*
Annual Occasions Count Down script- By JavaScript Kit
For this and over 400+ free scripts, visit http://www.javascriptkit.com
This notice must stay intact
*/

var today=new Date()

//Enter the occasion's MONTH (1-12) and DAY (1-31):
var theoccasion=new Date(today.getFullYear(), 5, 25)

//Customize text to show before and on occasion. Follow grammer below:
var beforeOccasionText="left until the 2007 Rolling Blue Thunder Black Bike
Week Meet & Greet"
var onOccasiontext="Today is the fday of the 2004 Rolling Blue Thunder Black
Bike Week Meet & Greet!"

var monthtext=new
Array("Jan","Feb","Mar","April","May","June","July","Aug","Sep","Oct","Nov","Dec")
theoccasion.setMonth(theoccasion.getMonth()-1) //change to 0-11 month format
var showdate="("+monthtext[theoccasion.getMonth()]+"
"+theoccasion.getDate()+")" //show date of occasion

var one_day=1000*60*60*24
var calculatediff=""

calculatediff=Math.ceil((theoccasion.getTime()-today.getTime())/(one_day))
if (calculatediff<0){ //if bday already passed
var nextyeartoday=new Date()
nextyeartoday.setFullYear(today.getFullYear()+1)
calculatediff=Math.ceil((nextyeartoday.getTime()-today.getTime())/(one_day)+calculatediff)
}

//Display message accordingly
var pluraldayornot=(calculatediff==1)? "day" : "days"
if (calculatediff>0)
document.write("<b>"+calculatediff+" "+pluraldayornot+"
"+beforeOccasionText+" "+showdate+"!</b>")
else if (calculatediff==0)
document.write("<b>"+onOccasiontext+" "+showdate+"!</b>")

</script>
 
"Pictures" <[email protected]> posted some javascript code...

Try the following:

<script type="text/javascript">
/*
* Annual Occasions Count Down script- By JavaScript Kit
* For this and over 400+ free scripts, visit
* http://www.javascriptkit.com
* This notice must stay intact
*
*/

var today = new Date();

//Enter the occasion's MONTH (1-12) and DAY (1-31):
var theoccasion = new Date(today.getFullYear(), 5, 25);

// Customize text to show before and on occasion. Follow grammer below:
var beforeOccasionText = "left until the 2007 Rolling Blue Thunder Black Bike Week Meet & Greet";
var onOccasiontext = "Today is the fday of the 2004 Rolling Blue Thunder Black Bike Week Meet & Greet!";

var monthtext=new Array("Jan","Feb","Mar","April","May","June","July","Aug","Sep","Oct","Nov","Dec");
//change to 0-11 month format
theoccasion.setMonth(theoccasion.getMonth()-1);
//show date of occasion
var showdate = "(" + monthtext[theoccasion.getMonth()] + " " + theoccasion.getDate() + ")";

var one_day = 1000*60*60*24;
var calculatediff = "";
calculatediff = Math.ceil((theoccasion.getTime() - today.getTime()) / (one_day));

if (calculatediff<0) {
// if bday already passed
var nextyeartoday = new Date();
nextyeartoday.setFullYear(today.getFullYear() + 1);
calculatediff = Math.ceil((nextyeartoday.getTime() - today.getTime()) / (one_day) + calculatediff);
}

//Display message accordingly
var pluraldayornot = (calculatediff==1) ? "day" : "days";

if (calculatediff > 0) document.write("<p style=\"text-align:center;font-weight:bold;\">" + calculatediff + " " + pluraldayornot + "
" + beforeOccasionText + " " + showdate + "!</p>");
else if (calculatediff == 0) document.write("<p style=\"text-align:center;font-weight:bold;\">" + onOccasiontext + " " + showdate +
"!</p>");
</script>

The two document.write statements at the bottom display
the information to the document. I enclosed them in some
<p> tags, removed the <b> tags and used CSS inside the
<p> tags (the style="" statements) for centering and font-
weight (bold text).

That should get the job done for you. You'll have to take
a look at what other tags enclose where the item gets displayed.
Watch the word wrap.

Let me know if you run into a problem.
 
Back
Top