Home >Web Front-end >CSS Tutorial >Use javascript to control the code of the target attribute of the link_Experience exchange

Use javascript to control the code of the target attribute of the link_Experience exchange

WBOY
WBOYOriginal
2016-05-16 12:06:561566browse

In HTML 4.0 Strict and XHTML 1.0 STRICT, the target attribute is not allowed to be used in the tag, which is a frustrating thing for web designers. It is still allowed in the transitional specifications. But through certain methods, we This problem can be solved.

The target attribute was removed from the HTMl4.0 specification. But it added another attribute: rel. This attribute is used to specify the relationship between the document containing the link and the linked document. Relational. Its attribute values ​​(such as: next, previous, chapter, section) are defined in the specification. Most of these attributes are used to define the relationship between small parts in a large document. In fact, in the specification Allow developers to freely use non-standard attribute values ​​for specific applications.

Here, we use a custom value external for the rel attribute to mark a link, which is used to open a new window.

Link code that does not comply with the latest web standards:
external link
Use the rel attribute:
external link
Now that we have constructed a link to a new window that complies with Web standards, we also need to use JavaScript to implement a new window. The script must be implemented The job is to find all the hyperlinks in the document that we define as rel="external" when the web page loads.

First we have to determine the browser.
if (!document.getElementsByTagName) return ;
getElementsByTagName is an easy-to-use method in the DOM1 standard, and it is supported by most browsers now. Because some old browsers such as Netscape 4 and IE4 do not support DOM1, we must determine this method Does it exist to exclude these old versions of browsers?

Next step, we get all the tags in the document through the getElementsByTagName method:
var anchors = document.getElementsByTagName("a"); anchors is assigned as an array containing each
tag. Now we must iterate through each tag and modify it:
for (var i=0; i var anchor = anchors;
}
Find the
tag to implement a new window
if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external" )
Next. Create the attribute value target and assign the value "_target":
anchor.target = "_blank";
Complete code:


function externalLinks() {  
          if (!document.getElementsByTagName)   
                  return;  
          var anchors = document.getElementsByTagName("a");  
         for (var i=0; i                 var anchor = anchors;  
                 if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")  
                             anchor.target = "_blank";  
         }  
}  
window.onload = externalLinks;

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