Results 1 to 5 of 5

Thread: Set href value using javascript at onload

  1. #1
    Join Date
    Jan 2010
    Posts
    126

    Set href value using javascript at onload

    I would like to set a value of an href on a link when the page is loaded. For that I think I need to set it using onload. I tried it as below but got stuck how should I move further now. Can anyone suggest me how should I move now?
    HTML Code:
    <head>
    <script>
        var hrefLnk = "webpage1.aspx"
        function setHref() {
        newhref.href == hrefLnk}
    </script>
    </head>
    <body onload="setHref()">
        <a id = "newhref" >
            SomeLinkText
        </a>

  2. #2
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Set href value using javascript at onload

    Code:
    function myHref() {
        document.getElementById('newhref').href == hrefLnk;
    }
    You can even use document.links array and perform the operation.

  3. #3
    Join Date
    Jan 2010
    Posts
    126

    Re: Set href value using javascript at onload

    Here is my code. I am just confused if I missed something, can you help me out?

    HTML Code:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="myServer">
        <title>New Page</title>
        <script>
        var lnkTitle = "This is the standard href title"
        var lnkTitle2 = "This is the alternate href title"
        var lnkTitle = "webpage1.aspx"
           
        function myHref() {
            document.getElementById('newhref').href == hrefLnk;}
        // alert(document.links('newhref').href)  
        // return hrefLnk}
       
        function jwriter(str){
        document.write(str )}
       
        </script>
    </head>
    <body onload="myHref()">
        <a id = "hrefLnk">
            <script type="text/javascript">
        jwriter(lnkTitle2)  
    </script>
     
    </a>
        <form id="form1" runat="myServer">
        <div>
       
        </div>
        </form>
    </body>
    </html>

  4. #4
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Set href value using javascript at onload

    I think you need to some changes as given below in your code:

    HTML Code:
    <script>
    
        var hrefLnk = "webpage1.aspx"
        function myHref() {
        document.links['newhref'].href == hrefLnk 
        }
       
    </script>
    
    <body onload="myHref()">
    
    ..
    
    <a  id='newhref'>Link newhref</a>
    </body>

  5. #5
    Join Date
    Jan 2010
    Posts
    126

    Re: Set href value using javascript at onload

    Ok I changed my code as you said and here is what I get. What happens in my case is that if I implement square brackets in the function line, I get "document.links.newhref.href is null or not an object" but however if I use parenthesis then it tells that "object required". What should I do now?

    HTML Code:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="myServer">
        <title>Untitled Page</title>
        <script>
       
        var lnkTitle = "This is the standard href title"
        var lnkTitle2 = "This is the alternate href title"
        var hrefLnk = "webpage1.aspx"
           
        function myHref() {
        document.links('newhref').href == hrefLnk}
        // alert(document.links('newhref').href)  
        // return hrefLnk}
       
        function jwriter(str){
        document.write(str )}
       
        </script>
    </head>
    <body onload="myHref()">
        <a id = "newhref">
            <script type="text/javascript">
        jwriter(lnkTitle2)  
    </script>
     
    </a>
        <form id="form1" runat="myServer">
        <div>
       
        </div>
        </form>
    </body>
    </html>

Similar Threads

  1. How do I cancel a href in javascript?
    By MAHESA in forum Software Development
    Replies: 6
    Last Post: 13-05-2010, 11:50 PM
  2. using href vs. onclick to call javascript function
    By Kalyug in forum Software Development
    Replies: 6
    Last Post: 12-05-2010, 10:09 AM
  3. href="javascript:func()" vs href="#" onclick="javascript:func()"
    By BansiJI in forum Software Development
    Replies: 6
    Last Post: 12-05-2010, 10:02 AM
  4. Firefox Javascript Href issue
    By Lalitmohan in forum Software Development
    Replies: 5
    Last Post: 11-05-2010, 10:26 PM
  5. Unable to target javascript location .href
    By Preetish in forum Software Development
    Replies: 3
    Last Post: 14-08-2009, 02:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,057,544.11707 seconds with 16 queries