Webpage opens new window target = _ blank does not comply with standards _ experience exchange

Source: Internet
Author: User
The common practice for opening a link in a new window is to add target & quot; _ blank & quot; behind the link, so there is no problem when we adopt a transitional DOCTYPE (xhtml1-transitional.dtd, but when we use a strict DOCTYPE (xhtml1-strict.dtd), this method will not pass W3C validation, the following error prompt will appear: "there is no attribute target for 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. We can only implement them 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 do not pass the user's consent, it is impolite to open a new window without a clear prompt. 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 target of the link. Rel has many attribute values, such as next, previous, chapter, and section. We want to use the rel = "external" attribute. The original code is as follows:


Open a new window

Now we need to write it like this: open a new window

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 ("");
For (var I = 0; I var anchor = anchors [I];
If (anchor. getAttribute ("href ")&&
Anchor. getAttribute ("rel") = "external ")
Anchor.tar get = "_ blank ";
}
}
Window. onload = externallinks;

You can save it as a. js file (such as external. js) and call it through the external connection method:

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.