casaschi
07-16-2010, 03:44 PM
Hello,
I came across a very odd browser behavior when trying to modify a css class using javascript and at the same time having a base html statement in my html file.
Without the base html statement, all browsers work fine and I can change the css class definition using javascript easily.
With a base html statement, only FireFox still works while Internet Explorer and Google Chrome dont work anymore. If there is a cross-domain issue, while one browser does work and the others dont?
An example of what I'm talking about, with the base statement:
http://freebsdcluster.org/~casaschi/tmp/example-base.html
Without the base statement:
http://freebsdcluster.org/~casaschi/tmp/example-nobase.html
Any idea how to tweak the code in the case with the base html statement in order for the javascript to work with all browser (modifying the class definition) ???
Keep in mind, I dont want a suggestion of another way to change the color, this is only an example. I want to be able to manipulare css classes with javascript when a base html statement is in my html code.
This is essentially the code:
<!--
-->
<base href='http://www.google.com'>
<style id='myStyle' type='text/css'>
.myStyle {
color: red;
}
</style>
<script type="text/javascript">
var lastColor = 'red';
function toggleColor() {
lastColor = lastColor == 'red' ? 'green' : 'red';
myObject = document.getElementById('myStyle');
if (myObject.sheet) { myObject.sheet.cssRules[0].style.color = lastColor; } // Mozilla style
if (myObject.styleSheet) { myObject.styleSheet.rules[0].style.color = lastColor; } // IE style
}
</script>
<a href="javascript: toggleColor();"><span class=myStyle>click here to toggle color</span></a>
Thanks in advance...
I came across a very odd browser behavior when trying to modify a css class using javascript and at the same time having a base html statement in my html file.
Without the base html statement, all browsers work fine and I can change the css class definition using javascript easily.
With a base html statement, only FireFox still works while Internet Explorer and Google Chrome dont work anymore. If there is a cross-domain issue, while one browser does work and the others dont?
An example of what I'm talking about, with the base statement:
http://freebsdcluster.org/~casaschi/tmp/example-base.html
Without the base statement:
http://freebsdcluster.org/~casaschi/tmp/example-nobase.html
Any idea how to tweak the code in the case with the base html statement in order for the javascript to work with all browser (modifying the class definition) ???
Keep in mind, I dont want a suggestion of another way to change the color, this is only an example. I want to be able to manipulare css classes with javascript when a base html statement is in my html code.
This is essentially the code:
<!--
-->
<base href='http://www.google.com'>
<style id='myStyle' type='text/css'>
.myStyle {
color: red;
}
</style>
<script type="text/javascript">
var lastColor = 'red';
function toggleColor() {
lastColor = lastColor == 'red' ? 'green' : 'red';
myObject = document.getElementById('myStyle');
if (myObject.sheet) { myObject.sheet.cssRules[0].style.color = lastColor; } // Mozilla style
if (myObject.styleSheet) { myObject.styleSheet.rules[0].style.color = lastColor; } // IE style
}
</script>
<a href="javascript: toggleColor();"><span class=myStyle>click here to toggle color</span></a>
Thanks in advance...