469,358 Members | 1,651 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

window.location and opera

hi guys,

this code works fine in IE but doesnot work in opera. the page just
reloads to test.html# in opera.

can anyone help? thanks.

-----------------------------------------------------------------------

<html>
<head><script language="JavaScript" type="text/JavaScript">
function disco() {
if( confirm('are you sure?') ) {
window.location="http://wwww.google.com";
}
}
</script>
</head>
<body>
<a href="#" onClick="javascript:disco()">D&eacute;connexion</a></body>
</html>
Jul 23 '05 #1
10 5154
Ivo
"K. S." wrote
this code works fine in IE but doesnot work in opera. the page just
reloads to test.html# in opera.

<html>
<head><script language="JavaScript" type="text/JavaScript">
function disco() {
if( confirm('are you sure?') ) {
window.location="http://wwww.google.com";
Try setting the location's href property directly:
window.location.href="http://wwww.google.com";
}
}
</script>
</head>
<body>
<a href="#" onClick="javascript:disco()">D&eacute;connexion</a>
onclick="disco();"

without the "javascript:" bit and preferably with an ending semicolon for
good manners.
</body></html>


I don't have opera but it 's always the same little things..
HTH
Ivo
Jul 23 '05 #2
K. S. wrote:
hi guys,

this code works fine in IE but doesnot work in opera. the page just
reloads to test.html# in opera.

can anyone help? thanks.

-----------------------------------------------------------------------

<html>
<head><script language="JavaScript" type="text/JavaScript">
function disco() {
if( confirm('are you sure?') ) {
window.location="http://wwww.google.com";
}
}
</script>
</head>
<body>
<a href="#" onClick="javascript:disco()">D&eacute;connexion</a></body>
</html>


Probably because you are not returning false from the onclick. Also, you
do not need the javascript: and that may also be part of Opera not
working with it:

<a href="noScript.html" onclick="disco();return false">.........</a>
Jul 23 '05 #3
K. S. wrote:
hi guys,

this code works fine in IE but doesnot work in opera. the page just
reloads to test.html# in opera.

can anyone help? thanks.

-----------------------------------------------------------------------

<html>
<head><script language="JavaScript" type="text/JavaScript">
function disco() {
if( confirm('are you sure?') )
window.confirm method requires two arguments

function disco(){
if(confirm('are you sure?','') ){.....

Mick

{ window.location="http://wwww.google.com";
}
}
</script>
</head>
<body>
<a href="#" onClick="javascript:disco()">D&eacute;connexion</a></body>
</html>

Jul 23 '05 #4
K. S. wrote:
this code works fine in IE but doesnot work in opera. the page just
reloads to test.html# in opera.

can anyone help? thanks.


Make it Valid HTML[1] first, and remove the "javascript:" nonsense,
see the FAQ[2].
PointedEars
___________
[1] <http://validator.w3.org/>
[2] <http://jibbering.com/faq/>
Jul 23 '05 #5
Lee
K. S. said:

hi guys,

this code works fine in IE but doesnot work in opera. the page just
reloads to test.html# in opera.

can anyone help? thanks.

-----------------------------------------------------------------------

<html>
<head><script language="JavaScript" type="text/JavaScript">
function disco() {
if( confirm('are you sure?') ) {
window.location="http://wwww.google.com";
}
}
</script>
</head>
<body>
<a href="#" onClick="javascript:disco()">D&eacute;connexion</a></body>
</html>

Since your onClick event handler does not return false, you're
telling the page to both:
a) set the location to google.com
b) follow the URL specified in the HREF value to "test.html#"

Browsers will do one or the other. Since following the link is
the primary function of the <A> tag, that's what most browsers
will do. If that's not what you want, return false:

onclick="disco();return false"


Jul 23 '05 #6
Mick White wrote:
K. S. wrote:
hi guys,

this code works fine in IE but doesnot work in opera. the page just
reloads to test.html# in opera.

can anyone help? thanks.

-----------------------------------------------------------------------

<html>
<head><script language="JavaScript" type="text/JavaScript">
function disco() {
if( confirm('are you sure?') )


window.confirm method requires two arguments

function disco(){
if(confirm('are you sure?','') ){.....


No, it really doesn't: <url:
http://devedge.netscape.com/library/...w.html#1201914
/>

It actually doesn't even require one argument in Internet Explorer: <url:
http://msdn.microsoft.com/workshop/a...ds/confirm.asp
/>

Although Gecko-based browsers generate an error and Netscape 4 presents a
confirm dialog that says "undefined" if you attempt to call confirm() without
any arguments. Regardless, that wasn't his problem. He explicitly said that
the link "reloads <the url>#". This is a major hint that he's got the
following code:
<a href="#" onClick="javascript:disco()">D&eacute;connexion</a></body>


and that he isn't returning false from the onclick event:

<a href="#" onclick="disco();return false;">Google</a>

Of course, this isn't really what he wants because regardless of what the
user chooses the link won't be followed. What he wants is for the link to
force the user to answer yes or no, so:

<script type="text/javascript">
function disco() {
return confirm("Are you sure?");
}
</script>
<a href="http://wwww.google.com" onclick="return disco();">Google</a>

Is what he really wants.

Now you've got a fully functional link regardless of whether the browser
supports or has JavaScript enabled. And if JavaScript is enabled, it will
prompt the user "Are you sure?". Clicking the affirmative button will result
in true being returned to the event and the HREF will be followed. Clicking
the negative button will result in false being returned to the event and the
HREF will not be followed.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #7
Grant Wagner wrote:
Mick White wrote:
window.confirm method requires two arguments

No, it really doesn't: <url:
http://devedge.netscape.com/library/...w.html#1201914
/>

It actually doesn't even require one argument in Internet Explorer: <url:
http://msdn.microsoft.com/workshop/a...ds/confirm.asp
/>
Well, "require" is probably the wrong word, "recommended" would be more
accurate.
Although Gecko-based browsers generate an error and Netscape 4 presents a
confirm dialog that says "undefined" if you attempt to call confirm() without
any arguments. Regardless, that wasn't his problem. He explicitly said that
the link "reloads <the url>#".


The answer to this problem was already pointed out to the OP.

Mick

Jul 23 '05 #8
Thanks all ...

Lee's post was the one that made it work.

for reference, here is the code that works:

----------------------------------------------------------------------
<html>
<head><script language="JavaScript" type="text/JavaScript">
function disco() {
if( confirm('are you sure?','') ) {
window.location.href="http://www.google.com";
}
}
</script>
</head>
<body>
<a href="#" onClick="disco();return false;">D&eacute;connexion</a></body>
</html>
Lee <RE**************@cox.net> wrote in message news:<ce********@drn.newsguy.com>...
K. S. said:

hi guys,

this code works fine in IE but doesnot work in opera. the page just
reloads to test.html# in opera.

can anyone help? thanks.

-----------------------------------------------------------------------

<html>
<head><script language="JavaScript" type="text/JavaScript">
function disco() {
if( confirm('are you sure?') ) {
window.location="http://wwww.google.com";
}
}
</script>
</head>
<body>
<a href="#" onClick="javascript:disco()">D&eacute;connexion</a></body>
</html>

Since your onClick event handler does not return false, you're
telling the page to both:
a) set the location to google.com
b) follow the URL specified in the HREF value to "test.html#"

Browsers will do one or the other. Since following the link is
the primary function of the <A> tag, that's what most browsers
will do. If that's not what you want, return false:

onclick="disco();return false"

Jul 23 '05 #9
Mick White wrote:
Grant Wagner wrote:
Mick White wrote:

window.confirm method requires two arguments

No, it really doesn't: <url:
http://devedge.netscape.com/library/...w.html#1201914
/>

It actually doesn't even require one argument in Internet Explorer: <url:
http://msdn.microsoft.com/workshop/a...ds/confirm.asp
/>


Well, "require" is probably the wrong word, "recommended" would be more
accurate.


Well, no, a second parameter for the confirm() method isn't "recommended" either. In both of the
links shown above confirm() takes exactly one parameter (well, in the case of IE, even that one
parameter is optional). The Gecko DOM is the same: <url:
http://www.mozilla.org/docs/dom/domr...2.html#1016997 />

Oh, it just occurred to me, you're thinking of prompt(), which does take a second (optional)
parameter.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #10
Grant Wagner wrote:


Oh, it just occurred to me, you're thinking of prompt(), which does take a second (optional)
parameter.
By golly, you're right, I am. Sorry to the OP for the confusion.
Mick

Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Peter Altenberg | last post: by
14 posts views Thread by Frances Del Rio | last post: by
18 posts views Thread by len.hartley | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.