Reply
Move Javascript in Body Tag to CSS sheet
Old 02-13-2008, 06:24 PM Move Javascript in Body Tag to CSS sheet
aschecht's Avatar
Extreme Talker

Posts: 180
Name: A
Location: San Jose, CA
I need to apply a javascript to the body tag of pages throughout my site as follows:

<body onload="highlight()";>

Can I do this instead with CSS to avoid including it in every body tag as follows:

Call a style sheet with

body
{
onload="highlight()";
}

and just use a regular <body> tag in every HTML page.

I tried it and it doesn't work. Is it supposed to or is this not possible? Then again, did I just do it wrong with bad syntax or something?

Andrew
aschecht is offline
Reply With Quote
View Public Profile Visit aschecht's homepage!
 
When You Register, These Ads Go Away!
Old 02-13-2008, 06:46 PM Re: Move Javascript in Body Tag to CSS sheet
tripy's Avatar
Fetchez la vache!

Posts: 2,171
Name: Thierry
Location: In the void
It's not supposed to be done this way, thus it's not possible.
But you can do that with javascript.

If you want something portable, the best way is to put the onload definition in an external js file, that you include on every page.
Add a DOM call to register the onload function in it:
Code:
try{
  document.addEventListener(window,'load',onloadFunction);
}
catch(error){
  document.attachEvent(window,'onload',onloadFunction);
}

function onloadFunction(){
  //Do what you want here, it's called when the page is loaded....
}
With this, every page that include a reference to that .js file
HTML Code:
<!--Put this into the <head> element-->
<script type="text/javascript" src="/path_to_the_file/functions.js"></script>
will register the function "onloadFunction()" as the one to be called.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 02-13-2008, 08:22 PM Re: Move Javascript in Body Tag to CSS sheet
aschecht's Avatar
Extreme Talker

Posts: 180
Name: A
Location: San Jose, CA
That answers the question perfectly! Thanks.

A.
aschecht is offline
Reply With Quote
View Public Profile Visit aschecht's homepage!
 
Old 02-13-2008, 11:48 PM Re: Move Javascript in Body Tag to CSS sheet
willcode4beer's Avatar
Webmaster Talker

Posts: 696
Name: Paul Davis
Location: San Francisco
Thierry gave you the right answer but...... it's not 100% correct.
Regardless of what you are about to read, DO FOLLOW Thierry's advice.

Internet Explorer supports a thing called behaviors. Behaviors are basically javascript files that are applied via CSS. THe primary use of behaviors is to fix broken/missing CSS in IE. Though, it is JS, so it can be used for evil. For an example, see my page on fixing the quote tag:
http://willcode4beer.com/tips.jsp?set=fixIEQuotes
For the most part, avoid behaviors.

Firefox and Opera also have some limited ability to apply javascript via CSS. The usage of this is very very narrow. Don't do it for general purpose scripts.
__________________
Paul Davis
willCode4Beer.com (coding for all the right reasons)
willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 02-14-2008, 04:48 AM Re: Move Javascript in Body Tag to CSS sheet
tripy's Avatar
Fetchez la vache!

Posts: 2,171
Name: Thierry
Location: In the void
Quote:
Firefox and Opera also have some limited ability to apply javascript via CSS. The usage of this is very very narrow. Don't do it for general purpose scripts.
Yeah, I knew it, but their support is so weak that I avoid it as much as I can.
I believe the only time I did this, was to implement a "position:fixed" in ie6, on an existing site that was so messed up that I could not trigger the browser in the right mode.

I had a bit of javascript in the css that was computing the "top" value on each events.
Brrrr, it gaves me shivers, just to think about it.
Talk about something messy...
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Move Javascript in Body Tag to CSS sheet
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13617 seconds with 12 queries