Forum Moderators: open

Message Too Old, No Replies

document.createElementNS

replacing document.write

         

QuadrupleX

6:41 pm on Sep 11, 2005 (gmt 0)

10+ Year Member



My site uses xhtml strict markup. However the pages, like most xhtml
documents, are served as text/html.

I have decided to take the plunge and start serving them as application/xhtml+xml.

To do this I need to make many changes. One of these changes involves
a piece of javascript that is used on every single page. It prints out a
copyright notice using document.write .... so that the copyright date is
always the current year:

function copy() {
var thetime=new Date();
var nyear=thetime.getYear();

if (nyear<=99)
nyear= "19"+nyear;

if ((nyear>99) && (nyear<2000))
nyear+=1900;

document.write('copyright © ' + nyear+' domain.com <a href="foo.htm">all rights reserved</a>');

}

True xhtml documents served with the application/xhtml+xml MIME type
are not allowed to use document.write. So this script is obsolete.

What I need to use in the place of doucument.write is document.createElementNS.

Anyone know how to make my copy() script work for xml?

Bernard Marx

8:44 pm on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IE still doesn't support createElementNS (and I'm not sure it supports "application/xhtml+xml" either.)

You don't need to create an element, just put one there and fill it later:

JS-----------------------------------------
function setCopyYear()
{
var year = new Date().getYear();
year = year<2000? year+=1900 : year;
document.getElementById('copyYear')
.appendChild(document.createTextNode(year));
}

HTML--------------------------------------

<p>copyright &copy; <span id="copyYear"></span> domain.com <a href="foo.htm">all rights reserved</a></p>

<!--
Put script block after element,
(..or call the script after window has loaded)
-->
<script type="text/javascript">
setCopyYear()
</script>

QuadrupleX

9:01 pm on Sep 11, 2005 (gmt 0)

10+ Year Member



Yes, that looks good. I'll give it a whirl.

You are right about IE ... I intend to use content negotiation for that.

Just a hobby site, so ...

QuadrupleX

10:45 pm on Sep 11, 2005 (gmt 0)

10+ Year Member



OK. I have that running nicely from an external js file.

Now, if I wanted to add more text to that (other than just the date) how would I go about that?

I'm thinking along the lines of my origianl script which printed out the site name and a link to a copyright notice.

Thankyou in advance. (I must get round to learning this stuff one day, so I don't need to beg for help lol)

Bernard Marx

11:06 pm on Sep 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



which printed out the site name and a link to a copyright notice.

It does! Have a look. They (presumeably) remain constant, so they're just written in HTML.