To open a link in a new window, add target = "_ blank" after the link. We use the transitional DOCTYPE (xh
No problem with tml1-transitional. dtd), but when we use a strict DOCTYPE (xhtml1-strict.dtd), this method
The following error message is displayed when the W3C verification fails:
"There is no attributeTargetFor this element (in this HTML version )"
Originally, in strictly DOCTYPE of HTML4.01, XHTML1.0, and XHTML1.1, target = "_ blank", target = "_ self", and other syntaxes are
Invalid. It can only be implemented using JavaScript.
A friend asked why target = "_ blank" is not allowed "? This attribute is very convenient. Well, I don't know what W3C experts think.
As far as I know, it is mainly about "ease of use and friendliness", because foreigners feel that they will open
A new window is impolite. Whether the cancellation is reasonable or not, let's look at the solution.
Rel attribute
HTML4.0 adds a new property: rel, which is used to describe the relationship between the link and the page containing the link, and the object opened by the link.
. Rel has many attribute values, such as next, previous, chapter, and section. We want to use rel = "externa
L "attribute. The original code is as follows:
<A href = "document.html" target = "_ blank">
Open a new window </a>
Now we need to write it like this:
<A href = "document.html" rel = "external"> open
New window </a>
This is a method that complies with the strict standard. Of course, a javascript code must be used in combination.
Javascript
The complete code JS is as follows:
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;
You can save it as a. js file (such as external. js) and call it through the external connection method:
<Script type = "text/javascript" src = "external. js"> </script>
That's it.
Finally, the target = "new" used on our website is allowed under the transitional DOCTYPE, but does not comply with the strict standard. Next time
During the revision, I will adopt the strict mode and change all target = "new" to rel = "external ".