Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to open all <a href> links on a page in new windows?

I have a bunch of <a href=".html"> links and want to make them all open in new windows.

I know I can do a search and replace all to add target="_blank" to all my <a href="..."> links.

However, is there a quick way, such as setting a CSS style, or adding some JavaScript code, to accomplish the same thing?

like image 386
Tina CG Hoehr Avatar asked Sep 02 '12 11:09

Tina CG Hoehr


People also ask

How do I get links to open in a new window?

Open in a new window To open a link in a new browser window, hold the Shift on then click the link or right-click the link and select Open link in New Window.

Is there a way to open all links on a page?

Just right click on the link and hold it to drag a box around the links. When you release the right click of your mouse, all those links will open in new tabs. Open links in new tabs are the default action of the add-on, but you can change it open in new windows.

What is the fastest way to open links in a new tab?

Press Ctrl and click a link. Opens the link in a new tab and switches to the newly opened tab. Press Ctrl+Shift and click a link. Opens the link in a new window.


2 Answers

If you have a page consisting of only links, consider <base target="_blank">. This opens every link in a new window (but also includes the targets of forms, unless overridden with <form target="_self">.

As others have shown, without modifying the HTML source, you can use Javascript to iterate through all <a> tags and add the target attribute or add an event listener that sets the target attribute dynamically.

like image 149
Lekensteyn Avatar answered Oct 07 '22 17:10

Lekensteyn


If you have jQuery it's simple

$("a").attr("target", "_blank"); 

Or regular Javascript

var links = document.links; for (var i = 0; i < links.length; i++) {      links[i].target = "_blank"; } 

As per @Lekensteyn's suggestion, without Javascript (added for Completeness)

<base target="_blank">. 
like image 20
Scott Avatar answered Oct 07 '22 16:10

Scott