Click here to Skip to main content
15,867,756 members
Articles / Web Development / HTML

AJAX but accessible links

Rate me:
Please Sign up or sign in to vote.
2.71/5 (5 votes)
15 Nov 20061 min read 27.7K   140   7  
A JavaScript tip to make on the same HTML link a JavaScript call or a classic navigate for a new tab/ new window/ disabled JavaScript tool.
<script type="text/javascript">

function SmartLink(evt, sender, ajaxJs)
{
var ctrlid = sender.id;
var ctrlhref = sender.href;
sender.href='';
var btn = evt.button;
var IE=navigator.appName=='Microsoft Internet Explorer';
if(!IE){ btn ++ ;} 
var classic = false;
if((btn < 2) && (!evt.ctrlKey) && (!evt.shiftKey))
{
eval(ajaxJs);
setTimeout("SmartLinkRestaureHref('"+ctrlid+"', '"+ctrlhref+"')",1);
}
else
{
sender.href=ctrlhref;
}
return false;
}

function SmartLinkRestaureHref(cid, url)
{
document.getElementById(cid).href = url;
}
</script>
<!-- [/reusable functions] -->
<script type="text/javascript">
function MyAjaxLink1()
{
// my ajax method
alert('i can make a ajax call here');
}
</script>

<a id="SmartLink1" href="http://www.google.com" onclick="SmartLink(event, this, 'MyAjaxLink1()');">my smart link 1</a>
<ul>
<li>a simple click on the link : javascript call</li>
<li>a simple click on the link without javascript : classic navigate</li>
<li>open the link on the a new tab/ new window : switch to classic navigate</li>
</ul>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions