Web Standard Learning a label

Source: Internet
Author: User
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".



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.