Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add target="_blank" to a link within a specified div?

Let's say I have the following code:

<div id="link_other">     <ul>         <li><a href="http://www.google.com/">google</a></li>         <li>             <div class="some_class">                 dsalkfnm sladkfm                 <a href="http://www.yahoo.com/">yahoo</a>             </div>         </li>     </ul> </div> 

In this case, the JavaScript would add target="_blank" to all links within the div link_other.

How can I do that using JavaScript?

like image 804
kakkalo Avatar asked Apr 29 '09 21:04

kakkalo


People also ask

How do you add a target _blank to a URL?

Conclusion. You can use the target="_blank" attribute if you want your users to click on a link that opens up a new browser tab. The target="_blank" attribute is used inside the opening anchor tag like this.

What happens if you add target _blank to a link?

target="_blank" is a special keyword that will open links in a new tab every time. target="blank" will open the first-clicked link in a new tab, but any future links that share target="blank" will open in that same newly-opened tab.

Can you add target _blank in CSS?

Unfortunately, no. In 2013, there is no way to do it with pure CSS.

How do I change the target of a link in HTML?

To change the target of a link in HTML, use the target attribute of the <a>… </a> tag. The target attribute can be used to open any link in a new tab, or the same tab, etc. Opens the linked page in a new tab.

How to use target=”_blank” in HTML?

HTML target=”_blank” Link Tutorial with Examples 1 HTML Link with <a> Element. The HTML <a> link element with target="_blank" is supported by all major modern web browsers like Google Chrome, Microsoft Edge, Internet Explorer, Mozilla Firefox, Safari, ... 2 target=”_blank” vs target=”_new”. ... 3 Secure target=”_blank”. ...

Which browsers support blank links in HTML?

HTML Link with <a> Element The HTML <a> link element with target="_blank" is supported by all major modern web browsers like Google Chrome, Microsoft Edge, Internet Explorer, Mozilla Firefox, Safari, and Opera. The syntax of the target="_blank" is like below.

How to create a link to another page in HTML?

HTML provides the <a> element or tag in order to create links to the other pages, URL, or part of the same page. While creating a link we can set the style of the link like open in the same browser tab or page or in a new browser window or tab.

Is there an attribute like target=_new in HTML5?

According to standards like HTML, HTML 5, HTML 4 etc there is no attribute like target="_new". But modern browsers provide applications about the target="_new" even not a standard.


2 Answers

/* here are two different ways to do this */ //using jquery: $(document).ready(function(){   $('#link_other a').attr('target', '_blank'); });  // not using jquery window.onload = function(){   var anchors = document.getElementById('link_other').getElementsByTagName('a');   for (var i=0; i<anchors.length; i++){     anchors[i].setAttribute('target', '_blank');   } } // jquery is prettier. :-) 

You could also add a title tag to notify the user that you are doing this, to warn them, because as has been pointed out, it's not what users expect:

$('#link_other a').attr('target', '_blank').attr('title','This link will open in a new window.'); 
like image 138
artlung Avatar answered Oct 03 '22 00:10

artlung


Non-jquery:

// Very old browsers // var linkList = document.getElementById('link_other').getElementsByTagName('a');  // New browsers (IE8+) var linkList = document.querySelectorAll('#link_other a');  for(var i in linkList){  linkList[i].setAttribute('target', '_blank'); } 
like image 20
Mike Robinson Avatar answered Oct 03 '22 02:10

Mike Robinson