homepage Welcome to WebmasterWorld Guest from 54.145.191.14
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
return false on JS confirm
Sandro87

5+ Year Member



 
Msg#: 4190314 posted 7:12 pm on Aug 21, 2010 (gmt 0)

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

 

daveVk

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4190314 posted 12:00 am on Aug 23, 2010 (gmt 0)

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

onclick="
return confirm_tip ... "
rocknbil

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



 
Msg#: 4190314 posted 5:18 pm on Aug 23, 2010 (gmt 0)

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>

Sandro87

5+ Year Member



 
Msg#: 4190314 posted 7:13 pm on Aug 21, 2010 (gmt 0)


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

rocknbil

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



 
Msg#: 4190314 posted 5:58 pm on Aug 22, 2010 (gmt 0)

<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>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved