Home >Web Front-end >CSS Tutorial >The web page opens in a new window target=_blank does not meet the standards_Experience exchange
"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>