|
Caio´s idea is much more semantically correct.
Actually, that is not true. The goal is not to open all external links in a new window. This is intended for things like help buttons, that need to pop up a window. rel="XXX" may be an appropriate way to do that, but I cannot immediately think of a semantically valid rel value that covers all cases.
Still, an interesting idea. Thanks for suggesting it. You can specify for example: 'rel="help"' and all the ones with help will be opened in a new window. Because it is a link which has a help relationship with the page which contains the link.
No. That deals with help files, which is just one case. What about all the others? I can think of at least half a dozen different types, and that is just off the top of my head.
In Moodle, most of these links are produced from a link_to_popup_window function, which does not know why it is being called. Then, if we want to make Moodle more semantically accurate we will need to know why it is being called.
For every call to the function "link_to_popup_window", we would have to categorize that link adding a "rel" parameter. Make a list of categories. And make them all use this new method. Just to point to a discussion I started about the related problem:
http://moodle.org/mod/forum/discuss.php?d=75515 This has been since resolved as part of the deprecation of the `link_to_popup_window` function.
You can now create a new popup_action and using OUTPUT->link to display it. This will automatically attach JS events to display the href in a popup. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.getAttribute("rel") == "external" ) { anchor.target = "_blank"; }
}
}
window.onload = externalLinks;
We can modify the function to set other values to the target attribute, mapping the "rel" value with the "target" one:
function externalLinks() {
var map = { "external" : "_blank", "same" : "_self" }
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.getAttribute("rel") == "external" ) { anchor.target = map[anchor.getAttribute("rel")]; }
}
}
window.onload = externalLinks;
If you don't want to use the target anyway, it's possible to use window.open instead:
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.getAttribute("rel") == "external" ) { window.open(anchor.getAttribute("href")); }
}
}
window.onload = externalLinks;
Hope it helps!