Forum Moderators: not2easy

Message Too Old, No Replies

How to add the following Javascript code to a .CSS document

The code given is for an HTML document.

         

JackR

3:21 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



I'm trying to add a Javascript clock to a .CSS/XHTML document, but the code provided is for an HTML document.

The instructions state: "Inside the <BODY> tag itself, throw in the following onLoad event handler, as follows:"

<body onLoad="show_clock()">

My question is: how do I modify this code to work in my .CSS/XHTML document?

John_Keates

10:51 pm on Jul 19, 2007 (gmt 0)

10+ Year Member



That's not possible. JavaScript is not embeddable in CSS.

lavazza

10:25 am on Jul 20, 2007 (gmt 0)

10+ Year Member



Is it REALLY Xhtml?

penders

5:00 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



....but the code provided is for an HTML document.

Like you suggest, the code actually in your show_clock() function may not be suitable for XHTML, regardless of how you are trying to call it.

Are you just wanting to separate your JavaScript from your HTML?

rocknbil

5:19 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can't you add this to the Javascript without breaking XHTML?

window.onload = function () { show_clock(); };

HarryM

5:41 pm on Jul 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use this OK on a site with XHTML 1.0 Transitional. It works and (as far as I remember) it validates in W3C.

<body onload="function()">

JackR

11:30 am on Jul 21, 2007 (gmt 0)

10+ Year Member



Thanks for the replies.

Let me clarify a few things. First, the doctype: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

Second, the code that I am instructed to insert into the HTML document is as follows:

<body onload="show_clock()">
<script src="liveclock.js" type="text/javascript"></script>

If I insert this into my XHTML document, it works just fine.

BUT, it won't validate:

1.Error Line 90 column 27: document type does not allow element "body" here.
<body onload="show_clock()">

2. Line 90 column 0: start tag was here.
<body onload="show_clock()">

HarryM

12:22 pm on Jul 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The validator may be objecting because it sees the <body> tag as being in the wrong place.

Have you tried putting your <script... </script> in your <head> section of the html? Or do you have a <head> ... </head> section? Or not closed the <head> section with a </head> tag?

JackR

2:35 pm on Jul 21, 2007 (gmt 0)

10+ Year Member



I've now found a way to make the page validate to XHTML 1.0 Strict, but can someone confirm that this is correct usage of XHTML:

</head>
<body onload="show_clock()">
<script src="liveclock.js" type="text/javascript"></script>

-Now on to problem 2: how do I get rid of the annoying IE7 pop-up:

To help protect your security, Internet Explorer has restricted this webpage from running scripts or ActiveX controls that could access your computer. Click here for options...

penders

6:14 pm on Jul 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you have is OK, but I think it would be more usual to have your <script> element within your <head> section, like:

<html> 
<head>
<title>Page Title</title>
<script src="liveclock.js" type="text/javascript"></script>
</head>
<body onload="show_clock()">
<!-- rest of page... -->
</body>
</html>

You could also remove the onload event handler from your HTML body element and add a line to the top of your included liveclock.js file, as rocknbil suggests - this does the same thing, but helps keep your JS and HTML separate.

-Now on to problem 2: how do I get rid of the annoying IE7 pop-up:

But "it's a feature of Microsoft Internet Explorer"! You get that 'security warning' in IE whenever you try to run JavaScript locally (ie. not on a webserver) - in the local security zone. This doesn't effect other browsers. If you were to upload your page to a website you won't get the error.

You can, however, get around the problem if you need to run JS locally (from a CD perhaps) by adding, what Microsoft calls the 'Mark Of The Web' (MOTW) to every webpage that executes JS. This takes the form of a special HTML comment you can place near the top of your HTML file (below the DOCTYPE) which forces the page to run in a different security zone:

eg:

<!-- saved from url=(0014)about:internet -->

For more info, Google for "Mark of the Web".

[edited by: penders at 6:16 pm (utc) on July 21, 2007]

rocknbil

6:14 pm on Jul 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how do I get rid of the annoying IE7 pop-up:

Are you accessing this locally, on your computer? That should go away when uploaded to the server.

JackR

9:22 pm on Jul 21, 2007 (gmt 0)

10+ Year Member



Thanks penders and rocknbil!

I have placed the <script> element within the <head> section, moved the onload event handler to the liveclock.js file AND disabled the IE7 Active X warning ... and the page validates!

If anyone wants to know how to turn off the ActiveX warning:

To disable the message, Start > Control Panel > Internet Options > Advanced (Tab) > CHECK Allow active content to run in files on My Computer (under Security) > OK

That's the simple part out of the way. Now, who can tell me how to place the javascript clock over the header image on the right-hand side of the page ...?

[edited by: JackR at 9:23 pm (utc) on July 21, 2007]

JackR

9:53 pm on Jul 21, 2007 (gmt 0)

10+ Year Member



To clarify, how do I make the javascript clock display at a given point within/over the header image contained in the div below?:

<div id="logo"><img src="img/image.jpg" alt="red widgets" width="955" height="94" /> </div>

lavazza

10:17 pm on Jul 26, 2007 (gmt 0)

10+ Year Member



[w3schools.com...]

CSS position Property
The position property places an element in a static, relative, absolute or fixed position...

(that page also includes info on Z-index)