Web page opens new window Target=_blank does not conform to standard _ experience Exchange

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

This article refers to the following articles:

Kevin Yank's "New-window links in a standards-compliant world"
"Standards-based replacement for target=" _blank "in External links"
2006.7.13 Update
Many portals of the homepage are all pop-up, I estimate at least in China this concept is not changed, in this case you do not need to add rel under each link, the full code is as follows:

<script type= "Text/javascript" >//<! [cdata[
function Externallinks () {
if (!document.getelementsbytagname) return;
var anchors = document.getElementsByTagName ("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors;
if (Anchor.getattribute ("href"))
Anchor.target = "_blank";
}
}
Window.onload = Externallinks;
]]></script>

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.