Details
-
Type:
Sub-task
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 2.0
-
Fix Version/s: 2.0
-
Component/s: Accessibility
-
Labels:None
-
Affected Branches:MOODLE_20_STABLE
-
Fixed Branches:MOODLE_20_STABLE
Description
So we can get the effect of target="_blank" without invalid HTML.
http://dialnet.unirioja.es/js/procesarLinks.js may have useful inspiration.
Issue Links
| This issue has been marked as being related by: | ||||
| MDL-17701 | When writing a web address in plain text, it will translate it to a <a> element with the parameter "target" which is not allowed in XHTML 1.0 Strict |
|
|
|
Yes, you can do that. In my opinion, the best way is to add a 'rel="external"' attribute to the link tag. And you can achieve the effect of target="_blank" with the following Javascript function:
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!