_ Does the new blank window meet the standard ?, _ Blank new window

Source: Internet
Author: User

_ Does the new blank window meet the standard ?, _ Blank new window

The common practice for opening a link in a new window is to add target = "_ blank" behind the link, and we use a transitional DOCTYPE (xh tml1-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 attributeTargetFor 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. Well-developed 5-year UI front-end framework!

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 object opened by the link. Rel has many attribute values, such as next, previous, chapter, and section. We want to use the rel = "externa l" attribute. The original code is as follows:

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

Now we need to write it like this:

<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:

. Code
  1. Function externallinks (){
  2. If (! Document. getElementsByTagName) return;
  3. Var anchors = document. getElementsByTagName ("");
  4. For (var I = 0; I <anchors. length; I ++ ){
  5. Var anchor = anchors [I];
  6. If (anchor. getAttribute ("href ")&&
  7. Anchor. getAttribute ("rel") = "external ")
  8. Anchor.tar get = "_ blank ";
  9. }
  10. }
  11. Window. onload = externallinks;

 

 

 

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

<Script type = "text/javascript" src = "external. js"> </script>

That's it. Well-developed 5-year UI front-end framework!

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 ".


If the target = "_ blank" is opened in a new window

Change the following function to the following code.
<Script language = "javascript">
Function searchit ()
{
Var searchwords = document. getElementById ("searchwords"). value;
If (document. getElementsByName ("search") [0]. checked = true)
{
Window. open ("www.baidu.com/s? Wd = "+ searchwords)
}
Else
{
Window. open ("www.google.cn/search? Q = "+ searchwords)

}
}
</Script>

How can I not open a new window when target = "_ blank" is added to the webpage?

Tested. No problem.

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.