tags:

views:

202

answers:

2

hi everyone, im trying to create a dynamic page using external .css pages where the page color will get changed below is my code.. but when i click the "href" i am not getting any output.. can anyone pls tell whats the problem in my code.. any sugessions or ideas pls.. thanks in advance...

<script language="JavaScript">
function loadjscssfile(filename, filetype)
{
  if (filetype=="css")
{ 
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

loadjscssfile("mystyle.css", "css") 
</script>
<a href="javascript:loadjscssfile('oldstyle.css','css')">Load "oldstyle.css"</a> 

hi everyone, i have modified my code as below still im facing problem in getting output.. no result .. can anyone pls help me out.. thanks in advance.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/newstyle.css" />
<script language="JavaScript" type="text/javascript">
function loadjscssfile(filename, filetype)
{
 if (filetype=="css") 
 {
var fileref = document.createElement("link");
fileref.rel = "stylesheet";
fileref.type = "text/css";
fileref.href = "filename";
document.getElementsByTagName("head")[0].appendChild(fileref)
}
}
loadjscssfile("oldstyle.css", "css") 
</script>
<a href="javascript:loadjscssfile('oldstyle.css','css')">Load "oldstyle.css"</a> 
</head>
A: 

If you want to switch a style of site on some instance (click), without refreshing page, then you need to use JS and cookies both.

May be below link will helpful:

http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm

santosh
A: 

I suggest that you use jquery for this.

Found this great article on how it can be done:

Happy coding

Muleskinner