Quote:
|
Originally Posted by carbonize
How would I do a link that uses javascript to open a new window but at the same time degrades so that it's a normal link when they have javascript turned off?
|
I read a good article about how to do it. I put an example up at
http://www.soltec.net/~dmg/external/index.html. The links on this page go to the same document, but one link is structured differently. The normal link is just a simple "href=", but the external has the attribute
rel="external" added like this:
The "rel" attribute is intended to specify the relationship between the document that contains the link, and the target of the link. We all know the standard types of this for linking to style sheets, next, previous, etc., but the specs allow any custom value you want for site-specific purposes. This is just what is needed for compliant targeted windows.
In the head of the document, an external javascript is called. It's done this way because it is assumed you would want the external links function available from several pages. The head of the page has this call to the JavaScript file:
Code:
<script type="text/javascript" src="external.js">
</script>
The external.js script file has this content:
Code:
function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
}
window.onload = externalLinks;
The 'externalLinks' function is called when the page loads. It then adds the target attribute, in a standards-compliant manner, to the links that have the 'rel="external"' attribute. It would seem that this is cheating, in a way, but since the target attribute is used only through the DOM in JavaScript, it is according to specs.
Try out the external link on the page with JavaScript turned off and turned on. It has the desired effect of a new window when on, and a normal link when off.