Changing the href of all links on a page.

Discussion in 'JavaScript' started by blueparukia, May 17, 2008.

  1. #1
    Basically, how do you do it?

    I'd asume it has something to do with:

    document.getElementsByTagName('a')

    and a for each, but I am not sure exactly how that would work. Basically I want to change all the hrefs of all the links on my page to the same thing.

    Thanks,

    Josh
     
    blueparukia, May 17, 2008 IP
  2. jnwms

    jnwms Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I haven't had a chance to check this but it should work

    
    for(x in document.links) {
    	document.links[x].href = 'yourlink.html';
    }
    
    Code (markup):
    this would change all the link on that page to the same url.

    is that what you wanted to do
     
    jnwms, May 17, 2008 IP
  3. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #3
    Aye, that is what I want it to do, but that code does not seem to work. I do have onlcick values for all my links too, so my current code is:

    
    function lockLinks(){
    for(x in document.links) {
    	document.links[x].href = 'http://google.com';
    	document.links[x].onclick = '';
    }
    alert("Links locked")
    }
    Code (markup):
    Thanks
     
    blueparukia, May 17, 2008 IP
  4. jnwms

    jnwms Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i've had a chance to have a look at the script and tested it in both IE and firefox and both seem to be working fine.

    put the following code ina new .html file and have a look for yourself

    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <script>
    function locklinks() {
    	for(x in document.links) {
    		document.links[x].href = 'javascript:void(0)';
    		document.links[x].onclick = '';
    	}
    }
    function dummyLinkFuction() {
    }
    </script>
    
    <body>
    <a href="http://forums.digitalpoint.com/showthread.php?t=845622">link</a>
    <br />
    <a href="http://forums.digitalpoint.com/showthread.php?t=845622">link</a>
    <br />
    <a href="http://forums.digitalpoint.com/showthread.php?t=845622">link</a>
    <br />
    <a href="http://forums.digitalpoint.com/showthread.php?t=845622">link</a>
    <br />
    <a href="http://forums.digitalpoint.com/showthread.php?t=845622">link</a>
    <br />
    <a href="http://forums.digitalpoint.com/showthread.php?t=845622">link</a>
    <br />
    <a href="http://forums.digitalpoint.com/showthread.php?t=845622">link</a>
    <br />
    <a href="http://forums.digitalpoint.com/showthread.php?t=845622">link</a>
    <br />
    <input type="button" onclick="locklinks();" value="Stop Linking" />
    </body>
    </html>
    
    Code (markup):
    Jon
     
    jnwms, May 17, 2008 IP
  5. jnwms

    jnwms Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    oh the dummy link function isn't ment to be opps

    Jon
     
    jnwms, May 17, 2008 IP