Opening link in new window
I am working on a CMS site that uses dynamic navigation. There is one link on the site that I would like to be able to open in a new window. However, since this is a dynamic environment I can't a开发者_如何学Godd the standard, target="_blank" to the link. so how can i open the link in new window without using jquery ?
$("#linkid").attr("target", "_blank");
Or to target by a css style $(".linkclass")
You can right click on the link and press "Open in new Window/Tab"
Seriously, you shouldn't overwrite the default behavior of the browser nor force the user to open a link in a new window. If the user want to open the link in a new window he already know how to do it (by selecting the entry in the popup menu, or even use a mouse gesture). But opening a link in the same window if the link is "forced" to open in a new window (with target="_blank"
) is much more difficult, specially if there is javascript involved (I really hate links like javascript:showDetails('12453563');
)
For all links on the page without jQuery but with Javascript:
links=document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
links[i].target="_blank";
}
For just the link that you want, if it is giving a unique ID:
link=document.getElementByID("link_id");
link.target="_blank";
For the link you want with no ID provided, if you know the URL:
links=document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
if(links[i].href = "http://the/url/you/are/targeting") {
links[i].target="_blank";
}
}
For the link you want with no idea provided, if there's a given class associated with it:
links=document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
if(links[i].className = "blankLinkClass") {
links[i].target="_blank";
}
}
精彩评论