| |
|
 |
New windows without using the target attribute by: sm0ke |
Page: 2 of 2 (View All) |
[b]Javascript rel=external[/b]
The following function was written by Roger Johansson, www.456bereastreet.com.
Save this file as jstarget.js
[code]
/*
JSTarget function by Roger Johansson, www.456bereastreet.com
*/
var JSTarget = {
init: function(att,val,warning) {
if (document.getElementById && document.createElement && document.appendChild) {
var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
var strWarning = ((typeof warning == 'undefined') || (warning == null)) ? ' (opens in a new window)' : warning;
var oWarning;
var arrLinks = document.getElementsByTagName('a');
var oLink;
var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
for (var i = 0; i < arrLinks.length; i++) {
oLink = arrLinks[i];
if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {
oWarning = document.createElement("em");
oWarning.appendChild(document.createTextNode(strWarning));
oLink.appendChild(oWarning);
oLink.onclick = JSTarget.openWin;
}
}
oWarning = null;
}
},
openWin: function(e) {
var event = (!e) ? window.event : e;
if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
else {
var oWin = window.open(this.getAttribute('href'), '_blank');
if (oWin) {
if (oWin.focus) oWin.focus();
return false;
}
oWin = null;
return true;
}
},
/*
addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
*/
addEvent: function(obj, type, fn) {
if (obj.addEventListener)
obj.addEventListener(type, fn, false);
else if (obj.attachEvent) {
obj["e"+type+fn] = fn;
obj[type+fn] = function() {obj["e"+type+fn]( window.event );}
obj.attachEvent("on"+type, obj[type+fn]);
}
}
};
JSTarget.addEvent(window, 'load', function(){JSTarget.init("rel","external"," (external website, opens in a new window)");});
[/code]
You can remove the "Warning opens in a new window" by editing line 6 and the final line.
To call the script into the html document you want to create the links in put the following between the tags:
[code]
[/code]
And finally the link is set as:
[code]
Link to Some Site
[/code]
So, weather you like opening links in new windows or not, at times its unavoidable and you now have 2 new ways to accomplish it if using the XHTML Strict doctype, or even if you just want to stop using target tags. The seccond method is more expandable than the first, you can add to it and make the script your own, but please post any major additions or improvements to the original author linked above.
No Comments for this page.
|
 |
Tutorial Stats |
| 535 |
Views |
| 0 |
Total Comments |
| 0 |
Rating of 5 ( Votes) |
|