/    Sign up×
Community /Pin to ProfileBookmark

The Insane Power of alert()

I am trying to write a script that takes you to a certain website depending on the kyepress, and I’m really really confused ?

The only difference between these two scripts is an alert box. Why does one work and the other not?
Sample 1 (this doesn’t work):

[CODE]
<html>
<head>
<script language=”JavaScript” type=”text/javascript”>
function ev(e)
{
var key = window.event ? e.keyCode : e.which;
if (key==13){
go();
}
}
function go () {

parent.location =”http://google.com”;

}
</script>
</head>
<body>
<form>
<input type=”text” onkeypress=”ev(event);” />
</form>
</body>
</html>[/CODE]

Sample 2 (this works):
EDIT: Interestingly, this works only if you wait for the new page (google) to load before clicking ‘ok’ on the alert box…If you click ‘ok’ before it finishes loading, it takes you back to the original page. ? ?

[CODE]<html>
<head>
<script language=”JavaScript” type=”text/javascript”>
function ev(e)
{
var key = window.event ? e.keyCode : e.which;
if (key==13){
go();
}
}
function go () {

parent.location =”http://google.com”;
alert(“Why does this work?”)
}
</script>
</head>
<body>
<form>
<input type=”text” onkeypress=”ev(event);” />
</form>
</body>
</html>[/CODE]

How does a simple alert box make all the difference?

Thanks,

sphoenixee

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@netbuddyJun 18.2006 — try...

self.location.href

see if it makes any difference.
Copy linkTweet thisAlerts:
@David_HarrisonJun 18.2006 — Probably because enter will submit the form (although there is no form, that's what enter does in a text box) and you are taken to whatever.html[color=red]?[/color], note the question mark that wasn't there before.

However, if you throw in an alert box, the form cannot be submitted until the user has OK'd it, but in the mean time the page is changed to google, so when the alert is OK'd, it's too late.
Copy linkTweet thisAlerts:
@phpnoviceJun 18.2006 — The reason has to do with the fact that you are pressing Enter in a form text box. This causes the form to be submitted. Since you have no form action specified, the default action is to submit the page to itself. Such an action will prevent navigation if you don't have the alert -- and wait until the loaded page has wiped out the document so that it can't be submitted to itself. ?

The easy solution? Change your form tag as follows:

<form onsubmit="return false">
Copy linkTweet thisAlerts:
@sphoenixeeauthorJun 18.2006 — Bingo, phpnovice! It works perfectly now. Many thanks to all those who responded so kindly and expediently!

sphoenixee
Copy linkTweet thisAlerts:
@phpnoviceJun 18.2006 — You're welcome.

Cheers.
×

Success!

Help @sphoenixee 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 5.17,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...