Web|web Standard
In Web Standard learning, we'll look at the next a tag today.
We're going to open the link in a new window The usual practice is to add target= "_blank" to the back of the link, and we have no problem with the transition type DOCTYPE (xhtml1-transitional. DTD), but when we use the strict DOCTYPE ( XHTML1-STRICT.DTD), this method will not pass the verification of the consortium, the following error message will appear:
"There is no attribute target to this element (in this HTML version)"
Originally in html4.01/xhtml1.0/xhtml1.1 strict doctype, target= "_blank", target= "_self" and so on syntax are invalid, we can only use JavaScript to adapt to achieve.
A friend asked why not allow the use of target= "_blank"? This property is very convenient. Hehe, I do not know what the experts of the Consortium think, as far as I know, the main problem is "ease of use, friendliness", because foreigners feel that without the consent of the user, it is impolite to open a new window without a clear hint. Let's take a look at the solution, regardless of whether the cancellation is reasonable or not.
Rel Property
HTML4.0 adds a new attribute: Rel, which describes the relationship between the link and the page that contains the link, and the target on which the link is opened. Rel has a number of property values, such as Next, previous, chapter, section, and so on. We're going to use the rel= "external" attribute. Code that was written like this:
<a href= "document.html" target= "_blank" > Open a new Window </a>
Now to write this:
<a href= "document.html" rel= "external" > Open a new Window </a>
This is a method that conforms to the strict standard. And, of course, it has to work with a JavaScript.
Javascript
The complete code JS is as follows:
function externallinks () {
if (!document.getelementsbytagname) return;
var anchors = document.getElementsByTagName ("a");
for (Var i=0 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 via an external join method:
<script type= "Text/javascript" src= "External.js" ></script>
That's it.
Finally, I use the target= "new" under the transition type DOCTYPE is allowed, but also does not meet the strict standard. Next revision I will adopt strict mode, all target= "new" to Rel= "external".