Forum Moderators: open

Message Too Old, No Replies

How to access a frame's styleSheets object?

         

JAB Creations

7:41 am on Apr 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The following script was written mostly by me though critical parts should be credited to Mark Wilton-Jones.

Any way it works perfectly fine except that I'd like to add support for frames. I know that in general you access a frame via parent.frame_name. I however am not sure how to access a frame's document.styleSheets object.

I can work the if frame/no frame if/else in, I just need to know where and how to apply it?

- John

function change_rule(selector,property,value)
{
var css_file_name = 'style';
if (document.styleSheets)
{
for (var oSsDmCt = 0; oSsDmCt < document.styleSheets.length; oSsDmCt++)
{
if (document.styleSheets[oSsDmCt].href.indexOf(css_file_name) + 1)// ¦¦ document.styleSheets[oSsDmCt].href.indexOf('style-broadband-images.css') + 1)
{
var oStyleSheet;
oStyleSheet = document.styleSheets[oSsDmCt]; break;
}
}
if (!oStyleSheet) {oStyleSheet = document.styleSheets[document.styleSheets.length-1];}
}
if (document.getElementById('coloreditor'))
{
var selector = document.forms['coloreditor'].step_one.value;
var property = document.forms['coloreditor'].step_two.value;
var value = '1';
if (property == 'background-color') {if (document.forms['coloreditor'].step_three_1.value != 'transparent') {var value = '#' + document.forms['coloreditor'].step_three_1.value;} else {var value = document.forms['coloreditor'].step_three_1.value;}}
else if (property == 'background-image') {
if (document.forms['coloreditor'].step_three_1.value != 'transparent') {var value = 'url(\''+path+ 'themes/general/background-'+document.forms['coloreditor'].step_three_2.value+'.gif\')';}
else {var value = 'none';}
}
else if (property == 'border-color') {if (document.forms['coloreditor'].step_three_3.value != 'transparent') {var value = '#' + document.forms['coloreditor'].step_three_3.value;} else {var value = document.forms['coloreditor'].step_three_3.value;}}
else if (property == 'color') {if (document.forms['coloreditor'].step_three_4.value != 'transparent') {var value = '#' + document.forms['coloreditor'].step_three_4.value;} else {var value = document.forms['coloreditor'].step_three_4.value;}}
}
var new_rule = selector + ' {' + property + ':' + value + ';}';

oStyleSheet.insertRule(new_rule,oStyleSheet.cssRules.length);
window.hasAddedNewRule = true;
}

*Edited for BB code error.

[edited by: JAB_Creations at 7:55 am (utc) on April 30, 2008]

JAB Creations

1:03 pm on May 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This works with additional coding to compensate for frames...
oStyleSheet = parent.frame_name.document.styleSheets[oSsDmCt];

- John