Hi
Hmm... My mind emphasized the "
exiting out of a website" part and quite ignored the "
button" word...
Sorry rac2, then my request for not using such things is addressed to Kelly.
rac2 said:
I am interested in understanding what your code does and how it permits tabbed browsing.
In extremely simple wording adds an [tt]onclick[/tt] parameter to the [tt]a[/tt] tags.
Code:
<a href="[URL unfurl="true"]http://example.com/">Example</a>[/URL]
[gray]|[/gray]
[gray]V[/gray]
<a href="[URL unfurl="true"]http://example.com/"[/URL] [red]onclick="return confirm('External link. Sure to follow ?');"[/red]>Example</a>
Code:
[gray]// do all this when the page finishes loading[/gray]
window.onload=[b]function[/b]() {
[gray]// get a list of all a tags in this document[/gray]
a=document.getElementsByTagName([i]'a'[/i]);
[gray]// keeps the current domain in a variable[/gray]
c=document.location.protocol+[i]'//'[/i]+document.location.host+[i]'/'[/i];
[gray]// do this for all a tags[/gray]
[b]for[/b] (i=0;i<a.length;i++)
[gray]// if the link's URL begins with 'http'[/gray]
[b]if[/b] (a[i].href.substr(0,4)==[i]'http'[/i]
[gray]// ... and does not begin with current domain[/gray]
&& a[i].href.substr(0,c.length)!=c)
[gray]// attach an inline function as onclick event handler[/gray]
a[i].onclick=[b]function[/b]() {
[gray]// ask visitor's confirmation an returns its value[/gray]
[b]return[/b] confirm([i]'External link. Sure to follow ?'[/i]);
}
}
It permits opening links in tabs by...
[ul]
[li]right click | Open Link in Wen Tab - does not fire [tt]onclick[/tt] event[/li]
[li]Ctrl-left click - fires the [tt]onclick[/tt] event and opens the document regardless the [tt]return[/tt] value[/li]
[li]middle click - does not fire [tt]onclick[/tt] event[/li]
[/ul]
rac2 said:
Also, please show HTML code for the button (input button, input image, or anchored image) you would be handling.
No, it does not touch the [tt]input[/tt] tags, as that was not may goal.
Supposing the document was loaded from [ignore]
[/ignore], the links will be affected like :
Code:
<a href="/">relative</a> [gray]<!-- nothing -->[/gray]
<a href="[URL unfurl="true"]http://example.net/">external</a>[/URL] [gray]<!-- adds the onclick function -->[/gray]
<a href="[URL unfurl="true"]http://example.com/">absolute</a>[/URL] [gray]<!-- nothing -->[/gray]
<a href="javascript:alert(0)">JavaScript</a> [gray]<!-- nothing -->[/gray]
But while you insist on [tt]button[/tt]s, is easy to modify the code to do its job on buttons. I mean [tt]input[/tt]s of [tt]type[/tt] [tt]submit[/tt], [tt]reset[/tt] and [tt]button[/tt].
Code:
window.onload=function() {
a=document.getElementsByTagName('input')
for (i=0;i<a.length;i++) if (a[i].type.match(/submit|reset|button/)) a[i].onclick=function() { return confirm('Really want to push that ?') }
}
[gray]<!-- ... -->[/gray]
<input type="text"> [gray]<!-- nothing -->[/gray]
<input type="radio"> [gray]<!-- nothing -->[/gray]
<input type="checkbox"> [gray]<!-- nothing -->[/gray]
<input type="submit"> [gray]<!-- adds the onclick function -->[/gray]
<input type="reset"> [gray]<!-- adds the onclick function -->[/gray]
<input type="button"> [gray]<!-- adds the onclick function -->[/gray]
Or to not affect all buttons, just some of them. Lets say those of class infactalink. ( Note that you do not have to define something for that style class. )
Code:
window.onload=function() {
a=document.getElementsByTagName('input')
for (i=0;i<a.length;i++) if (a[i].className=='infactalink' && a[i].type.match(/submit|reset|button/)) a[i].onclick=function() { return confirm('Really want to push that ?') }
}
[gray]<!-- ... -->[/gray]
<input type="button"> [gray]<!-- nothing -->[/gray]
<input type="button" class="infactalink"> [gray]<!-- adds the onclick function -->[/gray]
Feherke.