Click to See Complete Forum and Search --> : How can i set the value of href


havey
09-29-2003, 01:29 PM
How can i set the value of href in the last line below with the function determined value? So when texter function is called the <link> tag's href value is written to what the function derives it to be.


<script>
function texter(valu)
{
x = valu;
if (x = 1)
{
document.write ("set href in the line below to text1.txt");
}
if (x = 2)
{
document.write ("set href in the line below to text2.txt");
}
if (x = 3)
{
document.write ("set href in the line below to text3.txt");
}
}
</script>

<link rel="alternate" media="print" href="set this?">

</head>

Khalid Ali
09-29-2003, 01:34 PM
hoping that you only have one link element in a given document

var linkEl = document.getElementsByTagName("link")[0];
linkEl.setAttribute("href","whateverValue.css");

havey
09-29-2003, 01:50 PM
Khalid, you are very kind. Can you please explain how to use it and what it does.

Thank you.

havey
09-29-2003, 01:53 PM
I started reading over your script again, and again , and over again and this is what I think:
I don't need teh <link> tag cause i'm creating it as i go along so this is all i would use:

<script>
function texter(valu)
{
x = valu;
if (x = 1)
{
var linkEl = document.getElementsByTagName("link")[0];
linkEl.setAttribute("href","text1.txt");
}
if (x = 2)
{
var linkEl = document.getElementsByTagName("link")[0];
linkEl.setAttribute("href","text2.txt");
}
if (x = 3)
{
var linkEl = document.getElementsByTagName("link")[0];
linkEl.setAttribute("href","text3.txt");
}
}
</script>


</head>

Khalid Ali
09-29-2003, 01:58 PM
something like this should do it


<script>
function texter(val){
var linkEl = document.getElementsByTagName("link")[0];
if (val == 1){
linkEl.setAttribute("href","whateverValue1.css");
}else if (val == 2){
linkEl.setAttribute("href","whateverValue2.css");
}else if (val == 3){
linkEl.setAttribute("href","whateverValue3.css");
}
}
</script>

<link rel="alternate" media="print" href="">



Edit
Yep you've got it :D :D