The target = _ blank does not comply with the standard when the webpage opens a new window.

Source: Internet
Author: User

"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. Originally written in this wayCode:

<A href = "document.html" target = "_ blank">
Open a new window </a>

Now you need to write it as follows: <a href = "document.html" rel = "external"> open a 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 ("");
For (VAR I = 0; I <anchors. length; 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 one. JS files (such as external. <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. In the next revision, I will adopt the strict mode and change all target = "new" to rel = "external ".

This article references the followingArticle:

Kevin Yank's new-window links in a standards-compliant world
Standards-based replacement for target = "_ blank" in external links
2006.7.13 update
The home pages of many portal websites are all pop-up. I guess this concept cannot be changed for the moment at least in China. In this case, you do not need to add rel under each link. The full code is as follows:

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.