Links

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

21-Feb-11 13:20
So, I found some awesome code for making the links in ones website open in a new tab. It works, a little too well, I think. After a few days I realized that the same effect was being applied to the buttons in my *navigation bar*, lool.

Is there any way to restrict this to just specific links? http://philipos.com.


<script type="text/javascript">
function externalLinks()
{
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++)
{
var anchor = anchors[i];
if(anchor.getAttribute("href"))
anchor.target = "_blank";
}
}
window.onload = externalLinks;
</script>



Thanks again.


Philip
http://philipos.com
21-Feb-11 23:31
@Philip: Just put target="_blank" in the links that you want to open in a new window/tab:


<a href="page.html" target="_blank"> ... </a>


That's all that JavaScript is doing.

Cheers,
Matt

--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/

 
New posts
Old posts

Follow Elated