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?
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):
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.
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:
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...