


The web page opens in a new window target=_blank does not meet the standards_Experience exchange
May 16, 2016 pm 12:05 PM"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
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>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Principles and standards for PHP function library design

What criteria can be used to classify PHP functions?

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

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

What you need to know about general artificial intelligence

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

A Deep Dive into PHP's Standard Markup

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