FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

How to make CSS-changing JS target a frame

 


Hogwarts
Hello! I'm back from my leave of Frihost Smile

Well, I need help. I'm working on a script right now that changes the CSS in a frame. Such as this:
Code:
var css = "Css goes here..."

But, how do I make it target the frame?

Should be a simple thing to do, but I'm lost at how to do it :S

Thanks - Tobias
eznet
Can you address it like this with some action:

Code:
"javascript:top.frames['framename'].location = 'filename.html';return true;";
Aredon
addNewStyleSheetByFullCSS function definition
Code:

<script type="text/javascript">
function addNewStyleSheetByFullCSS(innerCSS,w){
var win=w||window;
  var h=win.document.getElementsByTagName("head");if(!h.length)return;
  var newStyleSheet=document.createElement("style");
  newStyleSheet.type="text/css";
  h[0].appendChild(newStyleSheet);
  try{
    newStyleSheet.styleSheet.cssText=innerCSS;
  }catch(e){try{
    newStyleSheet.appendChild(document.createTextNode(innerCSS));
    newStyleSheet.innerHTML=innerCSS;
  }catch(e){}}
}
</script>

usage
Code:

<script type="text/javascript">
function update_frameCSS(){
var css = document.getElementById("ta").value;
var frameRef=top.frames[0];
addNewStyleSheetByFullCSS(css,frameRef);
}
</script>
<textarea rows="20" cols="50" id="ta">Css goes here...</textarea>
<input type="button" value="Change Frame CSS" onclick="update_frameCSS()">


Do make sure that the two frames are within the same domain and server etc... lest you might run into the cross-server or cross-domain access denied security error thus forbidding JavaScript communication between the two.
dutchbob
Thank you for your example, it really helped me out!

However, the following two lines seem to try and do the same thing:

Code:

    newStyleSheet.appendChild(document.createTextNode(innerCSS));
    newStyleSheet.innerHTML=innerCSS;


The action of the first line are overruled by the actions of the second line.
Dropping the first line made the most sense to me.
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.