Home Web Front-end CSS Tutorial The web page opens in a new window target=_blank does not meet the standards_Experience exchange

The web page opens in a new window target=_blank does not meet the standards_Experience exchange

May 16, 2016 pm 12:05 PM
standard

"there is no attribute target for this element(in this HTML version)"

It turns out that under HTML4.01/XHTML1.0/XHTML1.1 strict DOCTYPE, target="_blank", target="_self" The syntax is invalid and we can only implement it through JavaScript.

A friend asked why target="_blank" is not allowed to be used? This attribute is very convenient. Haha, I don’t know what the W3C experts think. As far as I know, it is mainly about “ease of use and friendliness”, because foreigners think it is impolite to open a new window without the user’s consent or clear prompts. of. Regardless of whether this cancellation is reasonable or not, let’s look at the solution.

rel attribute
HTML4.0 adds a new attribute: rel. This attribute is used to describe the relationship between the link and the page containing the link, as well as the target of the link. rel has many attribute values, such as next, previous, chapter, section, etc. What we want to use is the rel="external" attribute. The code was originally written like this:


Open a new window


Now it is written like this:Open a new window

This is a method that complies with strict standards. Of course, it must be combined with a javascript to be effective.

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 You can save it as a .js file (such as external.js), and then call it through the external connection method: <script></script>

That's it.

Finally, the target="new" used by my website is allowed under the transitional DOCTYPE, but it does not meet the strict standard. I will use strict mode in the next revision and change all target="new" to rel="external".

This article refers to the following articles:

"New-Window links in a Standards-Compliant World" by Kevin Yank
"Standards-based Replacement for target="_blank" in External links 》
Updated on 2006.7.13
The homepages of many portal websites are all pop-up. I guess at least in China this concept cannot be changed for the time being. In this case, there is no need to add rel under each link. , the full code is as follows:

<script>//<![CDATA[ <BR>function externalLinks() {  <BR>    if (!document.getElementsByTagName) return;  <BR>    var anchors = document.getElementsByTagName("a");  <BR>    for (var i=0; i<anchors.length; i++){   <BR>        var anchor = anchors; <BR>        if   (anchor.getAttribute("href"))     <BR>        anchor.target ="_blank"; <BR>    } <BR>} <BR>window.onload = externalLinks; <BR>//]]></script>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Principles and standards for PHP function library design Principles and standards for PHP function library design Jun 16, 2023 am 11:37 AM

Principles and standards for PHP function library design

What criteria can be used to classify PHP functions? What criteria can be used to classify PHP functions? Apr 19, 2024 am 09:30 AM

What criteria can be used to classify PHP functions?

Analyzing the benchmarks and core requirements of sticky positioning: an in-depth discussion Analyzing the benchmarks and core requirements of sticky positioning: an in-depth discussion Jan 28, 2024 am 08:07 AM

Analyzing the benchmarks and core requirements of sticky positioning: an in-depth discussion

Does the Go language meet the standards of upper-level languages? Does the Go language meet the standards of upper-level languages? Mar 13, 2024 am 11:39 AM

Does the Go language meet the standards of upper-level languages?

What you need to know about general artificial intelligence What you need to know about general artificial intelligence May 09, 2023 pm 02:25 PM

What you need to know about general artificial intelligence

Demystifying the key elements of sticky positioning: How to evaluate the criteria for sticky positioning? Demystifying the key elements of sticky positioning: How to evaluate the criteria for sticky positioning? Jan 28, 2024 am 10:39 AM

Demystifying the key elements of sticky positioning: How to evaluate the criteria for sticky positioning?

A Deep Dive into PHP's Standard Markup A Deep Dive into PHP's Standard Markup Mar 29, 2024 pm 03:39 PM

A Deep Dive into PHP's Standard Markup

Standards for sticky positioning and analysis of elements and requirements for sticky positioning Standards for sticky positioning and analysis of elements and requirements for sticky positioning Feb 02, 2024 pm 12:36 PM

Standards for sticky positioning and analysis of elements and requirements for sticky positioning

See all articles