Hyperlinks in line with web standards (transfer)

xiaoxiao2021-03-06  40

Filed Under: Web Development X (HTML) - ISLI @ 11:09 AM

In HTML 4.0 Strict and XHTML 1.0 Strict, it is not allowed to use Target properties in the tag, which is an annoyed thing to make web designers. It is still allowed in the specification of the transition. But through a certain method, we You can solve this problem.

The Target property is taken in the HTML4.0 specification. But it adds another property: REL. This property is used to specify the relationship between the document containing the link and the link document. It defines its attribute value (such as : Next, Previous, Chapter, Section, most of these properties are used to define relationships between individuals in a large document. In fact. In fact. Specification allows developers to freely use non-standard attribute values ​​to do specific Application.

Here, we use a custom value External to mark a link for the REL attribute to open a window.

Link code does not meet the latest web standard:

External Link

Use the REL attribute:

External Link

Now we build a link to a new window that meets the web standard. We also need to use JavaScript to implement a new window. The work to be implemented is to find all those we define all those we defined in the document when the web is loaded. hyperlink of External.

First we have to judge the browser.

IF (! Document.getlementsBytagname) return;

getElementsByTagname is an easy way 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, so we must exclude this method These older versions of browsers.

Next, we get the tags in the document through the getElementsBytagname method:

VAR Anchors = Document.getElementsBytagname ("a");

Anchors is assigned to an array containing each label, now we must traverse each tag and modify it:

For (VAR i = 0; I

Var anchor = anchors;

Find the tag to achieve a new window

IF (ANCHOR.GETATTRIBUTE ("HREF") &&

Anchor.getaTribute ("Rel") == "External")

Next. Establish a property value Target and assign the value "_target":

Anchor.Target = "_blank";

Complete code:

---------------------------------------------------------------------------------------------------------------------------------------

Function externAllinks () {

IF (! Document.getlementsBytagname) return;

VAR Anchors = Document.getElementsBytagname ("a");

For (VAR i = 0; I

Var anchor = anchors;

IF (ANCHOR.GETATTRIBUTE ("HREF") &&

Anchor.getaTRibute ("Rel") == "external") Anchor.Target = "_blank";

}

}

Window.onload = extelLinks;


New Post(0)