javascript function problem

eon

2[H]4U
Joined
Oct 11, 2003
Messages
2,218
i cant figure out why my javascript function is not working. This isnt working but if i hardcode it in instead of getcss(), it works.
but doing other javascript stuff works like: document.getElementById("example").innerHTML='blahblah';

any ideas? thanks

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>



<script type="text/javascript">

function getcss()
{
  var temp = '../css/test.css'
  return temp;
}

</script>


<link href="getcss()" rel="stylesheet" type="text/css" />

....
blah blah
 
I think you're missing something in the href. It should be <a link href="javascript:getcss();" ...

I haven't code in awhile so I'm not sure if that's right. You can lookup calling javascript through href and you'll find a ton of answers.

Hope that helps.

-----

Edit: I didn't see that it's a link to a css file. I'm not sure if you can call it like that, and javascript:function() won't work.

I would like to hear other members about a possible work around.
 
Last edited:
everything about that is wrong. you can't put a javascript function anywhere you want. it doesn't work that way. javascript functions are usually run by browser events i.e. when the page loads or when you click a link.

if your goal is to load a CSS file via javascript, you should write a function that creates a new LINK tag, set the attributes accordingly, and then add it to the HEAD tag.
 
everything about that is wrong. you can't put a javascript function anywhere you want. it doesn't work that way. javascript functions are usually run by browser events i.e. when the page loads or when you click a link.

if your goal is to load a CSS file via javascript, you should write a function that creates a new LINK tag, set the attributes accordingly, and then add it to the HEAD tag.

well there are ways, but his way is totally wrong

http://www.cssnewbie.com/simple-jquery-stylesheet-switcher/
http://15daysofjquery.com/style-sheet-switcheroo/12/

OP, google for some more examples if these dont help
 
my bad wasnt thinking straight was put on a project very late friday night and im not a web developer. I know you can do stuff like onlick="javafunction()" so I assumed wrong it worked with similar calls.

im hoping just doing an innerHTML will work
 
I found a way doing this:
<script type="text/javascript">

var el = document.createElement("link");
el.setAttribute("rel","stylesheet");
if (v9700 > -1)
{
el.setAttribute("href","../css/test1.css");
}
else
{
el.setAttribute("href","../css/test2.css");
}

var h =document.getElementsByTagName("head")[0];
h.appendChild(el);

</script>

however now im thinking it may be better to just switch out which html page is loaded because there is a lot more objects and properties that need to have this conditional change than what I realized.
 
sounds like you are trying to do theming with css, look at jquery themes?
 
Back
Top