/    Sign up×
Community /Pin to ProfileBookmark

problem with javascript window

Hi all,

My code is like this:

<html>
<body>
<head>
<script type=”text/Javascript”>
function load(e){
if(!e){
e = window.event;
}
var button = e.which;
//document.write(button);
if(button==1) {
//document.write(“You have clicked left button”);
window.open(‘http://www.yahoo.com’,’_self’);
}
else if(button==2) {
//document.write(“You have clicked middle button”);
window.open(‘http://www.yahoo.com‘);
}
}
</script>
</head>
<body>
<a href=”http://www.google.com” id=”link” onMouseDown=”load(event);”>Google</a>
</body>
</html>

When i press middle click it works fine it is opening yahoo in a new window. But when i press left click it is opening google page. When i uncomment document.write it is displaying the message about the left button and opening yahoo in the same window. How can i modify the above code in so that the link opens in the same window with out the message and also not using document.href because when i press the back in the browser link display will change to yahoo in that case. I hope i am clear.

bye
kiran

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyNov 17.2008 — Change this:

onMouseDown="load(event);"

to this:

onMouseDown="return load(event)"

and change your function to this:
[CODE]function load(e){
if(!e) e = window.event;
var button = e.which;
if(button==1) {
window.open('http://www.yahoo.com','_self');
return false;
}
if(button==2) {
window.open('http://www.yahoo.com');
return false;
}
return true;
}[/CODE]
Copy linkTweet thisAlerts:
@kiransarvauthorNov 17.2008 — Thanks for your reply..

But it is not working....I am new to javascript. I have tried various things using window,location,top..but nothing worked. I am very curious that after removing comments on the line document.write() it is redirecting it to yahoo... Is there any reason???

regards

kiran
Copy linkTweet thisAlerts:
@MrNobodyNov 17.2008 — Yes, because [B]document.write()[/B] wipes out the rest of your document so the href link for google no longer exists. As for what I gave you, it works if you coded [B][I][U]both[/U][/I][/B] parts exactly as I gave it to you.
Copy linkTweet thisAlerts:
@kiransarvauthorNov 17.2008 — hi

sorry, i have just copied and pasted ur code and changed the onMouseDown action but it is not working.

Thank You

kiran
Copy linkTweet thisAlerts:
@MrNobodyNov 17.2008 — Well, if you're testing in IE, then you'll also need this change:
[CODE] var button = (e.which) ? e.which : e.button;[/CODE]
but, otherwise, I copied your code and made the two changes I outlined. Afterward, it worked fine.
×

Success!

Help @kiransarv spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...