Hide a link using js

Discussion in 'JavaScript' started by ruffneck119, Mar 15, 2011.

  1. #1
    I am trying to hide a link via javascript and it keeps popping up in a new window. How do I get my link to show up in the same window? Here is the code:

    
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    
    function url() {
    hidden = open('http://www.url.com');
    }
    
    // End -->
    </script>
    
    
    <A HREF="javascript:url()" ><IMG SRC="image.jpg" border="0"></A> 
    
    
    Code (markup):
    Can anyone help me?
     
    ruffneck119, Mar 15, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    By adding a target parameter:
    window.open('http://www.url.com', '_self');

    Or you can use this:
    window.location = 'http://www.url.com';

    This is suppose to be the best method, though I'm not entirely sure why.
    
    <html>
    <head>
    </head>
    <body>
    <a href="" id="mylink"><img src="image.jpg" border="0"></a>
    <script type="text/javascript">
    var link = document.getElementById('mylink');
    link.onclick = function () {
      window.location = 'http://www.url.com';
      return false;
    };
    </script>
    </body>
    </html>
    
    Code (markup):
     
    Cash Nebula, Mar 15, 2011 IP
  3. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #3
    Yes you are correct. but its more fun like this on your code:
    
    <a href="http://www.google.com" id="mylink"><img src="image.jpg" border="0"></a>
    
    Code (markup):
    I just changed "http://www.google.com". See the fun part of this. The user will see "http://www.google.com" but actually he/she will open "http://www.url.com".

    PS: The code will not work if the user tries to open your link in new tab cause all it do it is by JS.
     
    Tanya Roberts, Mar 16, 2011 IP
  4. ruffneck119

    ruffneck119 Active Member

    Messages:
    1,082
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Awesome let me try it out! Thanks for the help everyone.
     
    ruffneck119, Mar 18, 2011 IP
  5. zerophean

    zerophean Peon

    Messages:
    91
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if you want easy way, use jquery framework

    
    $(function(){
     $('#mylink,#anotherlink').click(function(){
       window.location = 'http://yourlink.com';
       return false;
     })
    });
    
    Code (markup):
    
    <a href="http://www.google.com" id="mylink"><img src="image.jpg" border="0"></a>
    <a href="http://www.google.com" id="anotherlink"><img src="image.jpg" border="0"></a>
    
    HTML:
     
    zerophean, Mar 18, 2011 IP
  6. ruffneck119

    ruffneck119 Active Member

    Messages:
    1,082
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Both work awesome thanks everyone!!
     
    ruffneck119, Mar 19, 2011 IP
  7. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Why would you want to do this? People can still find the target of the link if they want to and search engines will not index the link unless specified in a sitemap...
     
    camjohnson95, Mar 20, 2011 IP