views:

24

answers:

3

Is there a way (I assume it would be with javascript) that I can have a checkbox or link on my page that will make all the links on my page have target="_blank"?

I want to have a checkbox that says something like "Open all links in new page/tab" on my site that when checked will change the target and unchecked will put it back to how it was.

+2  A: 

jQuery example

$(function() {
    $('#yourCheckoxId').toggle(function() {
        $('a').attr('target', '_blank');
    },
    function() {
        $('a').removeAttr('target');
    });
});
Stephen
A: 

You might want to try jQuery as an alternative to genuine Javascript

the actual code could look something like that:

$('a').attr(target, '_blank')
poezn
A: 

Modifying the target attribute of all the anchors on the page is merely a matter of getting all links, and setting their target properties one by one:

var anchors = document.getElementsByTagName("a");
for(var i = 0; i < anchors.length; i++) {
    anchors[i].target = '_blank';
}
karim79