Javascript HTML concatenation

Hello experts,

Using javascript and HTML in SharePoint so I can't
use CSS to format text.

Using JavaScript to write date. Complete page
copied below.

In the segment

today =  "<font face='arial'>" + days[now.getDay()] + ", " +
         months[now.getMonth()] + " " +
         date + ", " +
         (fourdigits(now.getYear())) + "&lt; &#92;font &gt;";

I had been using
         (fourdigits(now.getYear())) + "<\font>";

but this displayed
< ont>
because it reads the backslash as an escape character
So I tried
"<&#92;font>";
but this displays
<\font>
So I tried  
"&#lt; <&#92; font &#gt;";
with the same result.

Is there some way of closing the tag in JavaScript?

Thanks

Allen in Dallas


<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE></TITLE>

<BODY>
<table  border="0">

<tr><td colspan="4" align="center">
<SCRIPT LANGUAGE="JavaScript">

// Get today's current date.
var now = new Date();

// Array list of days.
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

// Array list of months.
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

// Calculate the number of the current day in the week.
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

// Calculate four digit year.
function fourdigits(number)      {
      return (number < 1000) ? number + 1900 : number;
                                                }

// Join it all together
today =  "<font face='arial'>" + days[now.getDay()] + ", " +
         months[now.getMonth()] + " " +
         date + ", " +
         (fourdigits(now.getYear())) + "&lt; &#92;font &gt;";

// Print out the data.

document.write(today);

//
</script>


</td>
</tr>
</table>

</BODY>
</HTML>
Allen PittsBusiness analystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IronhoofsCommented:
Isnt it supposed to be </font> instead of <\font> ??
0
Julian HansenCommented:
I can't use CSS to format text.
Why not - there should be no restriction on this

<font> tag has been deprecated in HTML5 - you should avoid using it.

<\font> should be </font> as pointed out above

fourdigits can be replaced with Date.getFullYear - it does the same thing
Zero padding can be done by prepending "00" to getDate and then taking the right most 2 chars using .substr(-2)

Here is some code that does what you want.
Notes
Your original code contains quite a few old school conventions that have been deprecated - you might want to look at those.
There are other options for adding the date to the document - you can create a new <span> element for instance and assign a class to it and then style your class in your css.
You can use JQuery to set the styling on the element directly without a class.
Start with the code below and if it is not 100% what you need post back and we can explore some of the other options.

Working sample here
<!doctype html>
<html>
<head>
<title></title>
<style type="text/css">
.datestring {
  font-family: arial;
  font-size: 20px;
}
</style>
</head>
<body>
<table>
  <tr>
    <td class="datestring">
<script type="text/javascript">
// EXTEND DATE OBJECT TO INCLUDE A fullDate METHOD
Date.prototype.fullDate = function() {
  // AS PER ORIGINAL SCRIPT
  var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
  
  
  var today = days[this.getDay()] +  ", " + 
        months[this.getMonth()] + " " + 
        ("00" + this.getDay()).substr(-2) + ", " +  // TRICK TO ZERO PAD - TAKE RIGHT 2 CHARS OF DAY PREPENDED WITH '00'
        this.getFullYear();  // NO NEED FOR FOURDIGITS FUNC - THIS GETS THE YEAR AS FOUR DIGITS
  
  return today;
  
}
// CREATE THE DATE OBJET
var now = new Date();
// GET THE FULL DATE
today = now.fullDate();

// WRITE IT
document.write(today);

</script>
</td>
</tr>
</table>
</BODY>
</HTML> 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Allen PittsBusiness analystAuthor Commented:
Thinking that CSS could not be used because in SharePoint contributors do not have access to the
<head> section. But  will see if CSS can be used inline.
0
Julian HansenCommented:
Styles can be used inline - here is how you do it with a span.
document.write('<span style="font-family: arial; font-size: 20px; color: red">' + today + ' </span>');

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.