I'm trying to create a alert box which prompts user whether to delete or cancel, when I'm clicking on delete button it is working fine but when I click cancel again my webpage is closed. Below is the enclosed function. Please help.
<SCRIPT language="JavaScript">
<!--
function go_there()
{
var where_to= confirm("Do you really want to go to this page??");
if (where_to== true)
{
//closes the page
}
else
{
//Cancel the page and do not close the page
}
}
//-->
</SCRIPT>
In such cases the most common error might be that you would have omitted
return false
on clicking cancel, it should return false. This might be a plausible solution.
In your case either give a return statement or define an empty function which does nothing.
<SCRIPT language="JavaScript">
<!--
function go_there()
{
if (confirm("Do you really want to go to this page??")) {
//closes the page
} else
return false;
}
//-->
</SCRIPT>
<script language="JavaScript">
<!--
function go_there() {
if(!confirm('Do you really want to go to this page?')) {
return false; //Cancel the page and do not close the page
}
}
-->
</script>
Related
I have a problem with this script. When I press 'yes' it deletes the user which is fine. My problem is that it still deletes the user when I click cancel. Any ideas?
<script>
function myFunction() {
var x;
if (confirm("Are You Want To Delete This User ?") == true) {
x = "delete.php";
} else {
x = "memberlist.php";
}
document.getElementById("demo").innerHTML = x;
}
</script>
The mentioned JavaScript works fine. It may be that there is some issue in your delete.php or memberlist.php code. For a solution as per your expectations, please provide the code for delete.php and memberlist.php.
I assume your element is a link with "delete.php" as href - that is a VERY bad idea unless you want google spider to delete your database
You need to return false on a link's click event if you do not want it to execute - but see 1
I suggest
window.onload=function() {
document.getElementById("deleteLink").onclick=function() {
if (confirm("Do you really want to delete?") {
location="delete.php?user="+this.getAttribute("data-user");
}
}
return false // cancel link
}
using
Delete
Generic for more than one user:
window.onload=function() {
var deleteLinks = document.querySelectorAll(".deleteLink");
for (var i=0;i<deleteLinks.length;i++) {
deleteLinks[i].onclick=function() {
var user = this.getAttribute("data-user");
if (confirm("Do you really want to delete "+user+"?") {
location="delete.php?user="+user;
}
}
}
return false // cancel link
}
using
Delete Frank
Delete Bob
you can add confirm in your url
Edit : sory about spelling mistake i just want to show an alternative way i am texting on phone
<a onclick="return confirm('you are going to delet it are you sure bobo ?')" href="#your delet url">
In my program, if a user tries to leave a page, he'll receive a dialog box asking if he is sure he wants to leave.
How should I implement the 'cancel' option if the user chooses not to leave the page?
Source javascript code:
$(window).unload(function(){
var c= confirm ("Are you sure?");
if (c){
alert("Thanks. Good luck!");
}
else{
????
}
});
window.onbeforeunload = function() {
return 'You have unsaved changes!';
}
many question about this in stackoverflow
How can I override the OnBeforeUnload dialog and replace it with my own?
JavaScript + onbeforeunload
jQuery(window).on('beforeunload',function(){
var value = navigateAway(isDirty);
if(value=="changed")`enter code here`
{
if(jQuery("#saveCheck").val()=="false")
{
return "<?php echo JText::_('OVR_WANT_TO_LEAVE');?>";
}
}
});
I my web application I want to Hide/ Remove some for the menus like View Source, Properties using JavaScript coding. This should available in entire webpage.
Do not do it.
You only make your users angry but you can not stop them any way.
See also How do I disable right click on my web page?
If I can't stop you from doing it. You can only disable the whole menu:
<script language="javascript">
document.onmousedown=disableclick;
Function disableclick(e)
{
if(event.button==2)
{
return false;
}
}
</script>
var message="Sorry, right-click has been disabled";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {
if(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS;
}
else{
document.onmouseup=clickNS;
document.oncontextmenu=clickIE;
}
document.oncontextmenu=new Function("return false")
This is code disable the right click.
<a href="Profile.aspx?ProfileId=1" class="view-profile">View profile<a>
When the user click on my href there is an treatment=" .ajax script that will allow or the user to view the profile."
the problem is when that when the user right click my href,the treatment is ignored and the user can see the all profiles.
How can I prevent the right click?
Or how can I execute the same treatment(check) when the user right click my href.
I have tested the below code but not working:
$(function() {
$('.view-profile').bind("contextmenu", function(e) {
return false;
});
});
Thanks in advance.
You shouldn't rely on client-side validation to control user privileges, because it can be easily hacked; rather, use both server-side and client-side validation or simply server-side alone.
<script language=JavaScript>
<!--
//Disable right mouse click Script
//By Maximus (maximus#nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com
var message="Function Disabled!";
///////////////////////////////////
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}
function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return false")
// -->
</script>
--source: http://www.dynamicdrive.com/dynamicindex9/noright.htm
Once user presses refresh or browser back button,then I want to redirect the user to one of the page for restart the application.My javaScript code is as follow:
var workIsDone = false;
window.onbeforeunload =confirmBrowseAway;
function confirmBrowseAway()
{
if (!workIsDone) {
alert("You are moving to choice page");
window.location.href="/choice_page/";
}
}
function abc(){
workIsDone=true;
}
//.
//.
<div align="center"><input class="button blue" type="Submit" value="Next" onClick="abc()"></div>
But I don't know why it is not working. I don't want to use confirm box.
Followup:
I have updated the code a bit. Its taking the user to choice page and asking to remain on the choice page or carry on with the work. But when user click OK to carry on the work, the last submitted data is again submitted.
function confirmBrowseAway()
{
if (!workIsDone) {
window.location.href="/choice_page/";
return "Since you pressed refresh or back button, you are on start page again.";
}
}
You could try this, but it only works in IE (ie8 that I tested):
<script language="javascript">
function document.onkeydown() {
if (event.keyCode == 116) {
alert("MOVING AWAY");
window.location.href="/choice_page/";
event.keyCode = 0;
event.cancelBubble = true;
return false; }
}
</script>
Here is how to detect the refresh
Doing some here and there changes in the solution window.onbeforeunload may fire multiple times worked for me.
Thank you all,
Sunil Chauhan