zeqof
zeqof

Reputation: 5

Javascript's default event prevention not working

I'm trying to redirect the 'My Profile' menu link to a custom url when the user's browser has Javascript enabled. I've already coded a custom url creating dynamic function getNewURL() and the concatenation works fine. The problem is that even when Javascript is enabled (on my Chrome), the default page in the href loads, despite all the default prevention code. I've been spending hours on this, and can't figure out what the problem is. Any help would be greatly appreciated.

<head>
<script type="text/javascript">
<!--
function init() {
    document.getElementById('profile').onclick=getNewURL;
}

window.onload=function(){
init();
}



function getNewURL(e)
{
    if(!e) e = window.event;
    var a = 'http://www.google.com/'; 
    var b = 'advanced_search?hl=en';//this will actually be a dynamic wikispaces
             //variable - the username. 
    var url = a+b;

    window.location.href = url;

    //Over-riding default action
    //e.cancelBubble is supported by IE
    e.cancelBubble = true;
    e.returnValue = false;

    //e.stopPropagation works only in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        
    }
    e.preventDefault();
    return false;
}
//-->
</script>
</head>

<body>
<div id="menu">
<ul>
    <li><a href="http://www.google.com/" id="profile">My Profile</a></li>
</ul>
</div>
</body>        

Upvotes: 0

Views: 140

Answers (2)

epascarello
epascarello

Reputation: 207557

What is e?

function getNewURL()
{
    if(!e) var e = window.event;  <--

Should be

function getNewURL(e)
{
    if(!e) e = window.event;

Upvotes: 0

Roman Pominov
Roman Pominov

Reputation: 1433

Seems like you forgot to add parameter to function definition

function getNewURL(e) ...

Upvotes: 3

Related Questions