Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

CSS Forum


You are currently viewing our CSS Forum as a guest. Please register to participate.
Login



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

Posts: 190
Name: A
Location: San Jose, CA
Trades: 6
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!
 
 
Register now for full access!
Old 02-13-2008, 05:46 PM Re: Move Javascript in Body Tag to CSS sheet
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-13-2008, 07:22 PM Re: Move Javascript in Body Tag to CSS sheet
aschecht's Avatar
Extreme Talker

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

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

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
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.
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 02-14-2008, 03:48 AM Re: Move Javascript in Body Tag to CSS sheet
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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...
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Move Javascript in Body Tag to CSS sheet
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





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



Page generated in 0.24839 seconds with 12 queries