Welcome to WebmasterWorld Guest from 54.158.36.59

Forum Moderators: open

Message Too Old, No Replies

return false on JS confirm

     
7:12 pm on Aug 21, 2010 (gmt 0)

5+ Year Member



hi how can I return false so that the page wont go on the top when someone doesn't confirm a JS confirm?

  function confirm_tip(url, message){

if(confirm(message)){
window.location= url;
}

}





function confirm_tip(url, message){

if(confirm(message)){
window.location= url;
}
else{
return false;
}

}



this doesn't work
12:00 am on Aug 23, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Check how confirm_tip is being called, if it is onclick for example you will need

onclick="return confirm_tip ... "
5:18 pm on Aug 23, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Apparently we lost some posts this weekend. You want to return false in any case . . . remove the else. A more efficient way would be to 1) externally attach the behavior, and 2) use a valid URL. This would allow you to use the href attribute, it would still work for Javascript disabled visitors, you would un-bloat your link code and only need an ID, and if those three aren't enough, would allow search engines to follow your link. :-)


window.onload = function() {
if (document.getElementById('some-link')) {
document.getElementById('some-link').onclick=function() {
return confirm_tip(this.href,'Navigate to '+this.href+'?');
};
}
};
function confirm_tip(url, message){
if(confirm(message)){
window.location=url;
}
return false;
}


.....

<p><a href="somepage.html" id="some-link">Some Page</a></p>
7:13 pm on Aug 21, 2010 (gmt 0)

5+ Year Member




System: The following 2 messages were spliced on to this thread from: http://www.webmasterworld.com/javascript/4190317.htm [webmasterworld.com] by fotiman - 10:41 am on Aug 26, 2010 (utc -4)


hi how can I return false so that the page wont go on the top when someone doesn't confirm a JS confirm?

  function confirm_tip(url, message){

if(confirm(message)){
window.location= url;
}

}





function confirm_tip(url, message){

if(confirm(message)){
window.location= url;
}
else{
return false;
}

}



this doesn't work
5:58 pm on Aug 22, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



<p><a href="somepage.html" onclick="return confirm_tip(this.href,'Navigate to '+this.href+'?');">Some Page</a></p>


function confirm_tip(url, message){
if(confirm(message)){
window.location=url;
}
return false;
}

You want to return false in all cases, but have to "accept" it at the link level. Usage of the false return *and* an actual URL instead of # will allow the link to still work without Javascript, appreciated by both those who have it disabled *and* search engines. :-) A cleaner way is to externally attach the behavior:


window.onload = function() {
if (document.getElementById('some-link')) {
document.getElementById('some-link').onclick=function() {
return confirm_tip(this.href,'Navigate to '+this.href+'?');
};
}
};
function confirm_tip(url, message){
if(confirm(message)){
window.location=url;
}
return false;
}


<p><a href="somepage.html" id="some-link">Some Page</a></p>
 

Featured Threads

Hot Threads This Week

Hot Threads This Month