Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2003
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can i set the value of href

    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>

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use the setAttribute method to do this. Also, your code could be shortened to only this:

    Code:
    <script type="text/javascript">
    	function foo(bar)
    	{
    		document.getElementsByTagName("link")[0].setAttribute("href", "text"+bar+".txt");
    	}
    </script>
    Then, called like this:

    Code:
    foo("1");
    Which would result in the first <link> element's href attribute having the value of text1.txt.
    Last edited by fredmv; 09-29-2003 at 07:59 PM.
    Moderator @ WebDeveloper.com
    Mentor @ WebXpertz.net

  • #3
    Regular Coder
    Join Date
    Mar 2003
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Thank You very much

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Possible reasons for the error:

    • You called the function before the <link> element existed.
    • There was no <link> element defined.


    Here's a working example of my code:

    Code:
    <html>
    <head>
      <title>test</title>
      <link href="" />
      <script type="text/javascript">
      	function foo(bar)
     	{
    		document.getElementsByTagName("link")[0].setAttribute("href", "text"+bar+".txt");
    	}
    
    	function foobar()
    	{
    		alert("The current value of the first <link> element's href attribute is: " + document.getElementsByTagName('link')[0].getAttribute('href'));
    	}
      </script>
    </head>
    
    <body style="font-family: courier;">
    	<a href="#" onclick="foo('1');foobar();">foo('1');</a> |
    	<a href="#" onclick="foo('2');foobar();">foo('2');</a> |
    	<a href="#" onclick="foo('3');foobar();">foo('3');</a>
    </body>
    </html>
    Edited: Heh, I guess you figured it out. You're welcome.
    Last edited by fredmv; 12-19-2003 at 05:35 AM.
    Moderator @ WebDeveloper.com
    Mentor @ WebXpertz.net


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •