a.target='_blank';
Main Topics
Browse All TopicsWhere I might normally do in HTML:
<a href='...' onclick='alert(123);return
I am trying to replicate that with dynamically created tags. I want the href to be valid, but the onclick to return false so that it is executed and the browser does NOT navigate to the href'ed page. I can't manage that though. This creates the link just fine, and opens the window just fine, but it also navigates away from the original page which I want to avoid, when the window is opened.
<!doctype html public "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtm
<html xmlns="http://www.w3.org/1
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Page</title>
<style type="text/css">
</style>
<script type="text/javascript">
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//add event function from http://www.dynarch.com/pro
function addAnEvent(aEl, aEvName, aFunc) {
if (aEl.attachEvent) { // IE
aEl.attachEvent("on" + aEvName, aFunc);
} else if (aEl.addEventListener) { // Gecko / W3C
aEl.addEventListener(aEvNa
} else {
aEl["on" + aEvName] = aFunc;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function elFromEvent(aEvent) {
var el=null;
if (aEvent.target) el=aEvent.target;
if ('undefined'!=typeof event && event.srcElement) el=event.srcElement;
return el;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function showImage(aEvent) {
var el=elFromEvent(aEvent);
window.open(el.href);
return false;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//onload handler
addAnEvent(window, 'load', function() {
var a=document.createElement('
a.href='http://www.google.
addAnEvent(a, 'click', showImage);
a.appendChild(document.cre
document.body.appendChild(
});
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
</script>
</head>
<body>
</body>
</html>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: devicPosted on 2005-08-01 at 09:55:57ID: 14571706
replace
==============
addAnEvent(a, 'click', showImage);
with:
==============
a.target='blank';
;)