You need to sign in to do that
Don't have an account?
SFDC coder
show javascript popup when user navigates away from page
hi all,
i have a requirement where in i want to alert a user whenever the user navigates away from the webpage without clicking on save button and navigating away instead of that..i tried the below code but its not working..
below is my code
Can anyone please suggest as to how can i correct my code?
thanks,
i have a requirement where in i want to alert a user whenever the user navigates away from the webpage without clicking on save button and navigating away instead of that..i tried the below code but its not working..
below is my code
<apex:page controller="VistAudit_VFC" action="{!createData}"> <body onload="myfunction()"> <script> function myFunction() { alert("Are you sure you want to navigate away?"); } </script> <apex:form > <apex:outputLabel value="Audit Visit" style="font-family:Times New Roman;font-size:18px"></apex:outputLabel><br></br> <div align="center" draggable="false" > <apex:commandButton value="Save" action="{!save}" onclick="myFunction();"/> </div> .......... ........... ....... </apex:form> </body> </apex:page>
Can anyone please suggest as to how can i correct my code?
thanks,
Alternatively if you want to show a confirm message when they just leave the tab/page (lose focus) you need to use the "onblur" and "onfocus" events:
your first patch seems to be working for mee.
However just some minor issues
1.i get the pop up everytime the user navigates away,but when the user reloads and gets the pop up,then after i click on leave this page
it still reloads the same page.what can be the reason?
2.my pop up displays a boolean value false and then the message on the next line .How do i get rid of that boolean value?
thanks
thanks for your assistance... :)
but this is what i get...any help on this?
Still there are 2 issues..
1.the pop up arises,even when i click on my apex:command button save.I want it to pop up only when the user navigates away without saving anything.Is this doable?
2.The earlier issue i.e when the user clicks on F5 or reloads the page,the pop up arises and when clicked on leave this page,it reloads the same page again.
Can you suggest something over these two?
Thanks..
Hi SFDC coder,
one doubt you want that alert message in particular page alone ??
Thanks
D Naveen Rahul.
yes i want in that particular page only
Hi SFDC,
looks like this what you were expecting.
Mark it closed if this anwer helps you.
Thanks
D Naveen Rahul.
but still am facing those two issues
1.the pop up arises,even when i click on my apex:command button save.I want it to pop up only when the user navigates away without saving anything.Is this doable?
2.The earlier issue i.e when the user clicks on F5 or reloads the page,the pop up arises and when clicked on leave this page,it reloads the same page again.
just one question..from where do you get the value for variable e?which you have passed in function(e)?
You see am not good at all in javascript
Thanks..
The below will work like what you expecting now ,the only diference is the texts in the popup.
<apex:page standardController="Account" recordSetVar="accounts" sidebar="false">
<script>
window.twotimer= function() {
eventSrcID=(event.srcElement)?event.srcElement.id:'undefined';
eventtype=event.type;
status=eventSrcID+' has received a '+eventtype+' event.';
if(event.srcElement.innerHTML=='Accounts'){
if(confirm("Are you sure you want to navigate away from this page?"))
{
}else{
return false;
}
}
}
document.onclick= twotimer;
</script>
<apex:pageBlock >
<apex:pageBlockTable value="{!accounts}" var="a">
<apex:column value="{!a.name}"/>
<apex:column >
<apex:outputLink value="/apex/View_Account_detail?id={!a.Id}" id="theLink">View detail</apex:outputLink>
</apex:column>
<apex:column >
<apex:outputLink value="/apex/View_Account?id={!a.Id}" >View</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
my problem is not with the text now..its just that when i click on save button,also i get the pop up which is not an ideal solution.
the pop up should only arise when someone navigates away from the page
for eg:clicks on some other link or closes the tab or clicks backward/forward buttons on browsers and such actions
HI,
SDFC coder the codes work right
<script>
window.twotimer= function() {
eventSrcID=(event.srcElement)?event.srcElement.id:'undefined';
eventtype=event.type;
status=eventSrcID+' has received a '+eventtype+' event.';
i just hardcoded the link name below for account tab you can change it to what ever u need
if(event.srcElement.innerHTML=='Accounts'){
if(confirm("Are you sure you want to navigate away from this page?"))
{
}else{
return false;
}
}
}
document.onclick= twotimer;
</script>
my skype naveen_rahul for more detail.
Thanks
D Naveen Rahul.
i dont want to display the pop up when clicked on save button
highlighted above is my button..
Did you get a solution for this?
I want the popup to appear only when the user navigates away from the page without clicking the save button.
Thanks,