Safe way to access head style element

142 views
Skip to first unread message

kstubs

unread,
Nov 16, 2012, 1:12:17 PM11/16/12
to prototype-s...@googlegroups.com
What is the safe way to access the style element, or the head element for the document and then to append CSS?

I'm doing:
document.head.down('style').insert(some_style);

Safe (cross-browser)?  Or not?
Karl..

Jason Westbrook

unread,
Nov 16, 2012, 3:19:41 PM11/16/12
to prototype-s...@googlegroups.com

I tried this out in jsfiddle and it did work in Chrome - but I'm not sure if all browsers will update the styles on the page if the <style> tag is changed - though - they might cache the style references

but you can also access all of the style tags on the page using $$('style')

Jason Westbrook | T: 313-799-3770 | jwest...@gmail.com




--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To view this discussion on the web visit https://groups.google.com/d/msg/prototype-scriptaculous/-/SsjKLOTGjAsJ.
To post to this group, send email to prototype-s...@googlegroups.com.
To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.

Victor

unread,
Nov 20, 2012, 5:07:44 AM11/20/12
to prototype-s...@googlegroups.com
Get head element in pure JS:
var head = document.head || document.getElementsByTagName("head")[0];

Get first style element in pure JS:
var style = document.getElementsByTagName("style")[0];

Get first style element in Prototype JS:
var style = $$("style").first();

Modification of text in existing style element IMHO may be different in IE and W3C-compliant browsers - IE can modify content of style via property style.styleSheet.cssText, W3C browsers via DOM style.childNodes[0] or property style.innerText.

kstubs

unread,
Nov 21, 2012, 3:06:16 PM11/21/12
to prototype-s...@googlegroups.com
Excellent.  Thanks for the replies.
Reply all
Reply to author
Forward
0 new messages