Confirm close window popup does not appear on first time - javascript

I am trying following code to confirm user if they really want to close window.
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
</script>
But it does not work when my page load and I try to close page [X] without clicking anywhere on page. Yes it does appear when I click somewhere on page then close [X] window. I don't know why do I need to click somewhere on page and then close window.
What I tried so far but nothing works.
window.focus() on document ready as it gets focus on window.
$('#someElementOnpageId').click() as it gets auto click on document
ready.
Can anyone please suggest what should I do?

Related

How to prevent user to go back with right click on back button?

When I press and hold the browser back button or just right click the back button I can go back pages. But I need to disable it. Is there any way to do that with javascript or jsp
Open A New Window without Back Button
browser-back-button-viralpatelThis one is very crude technique. But in some case it works like charm. All you have to do is to open the webpage in a new window. This window doesn’t have back button at all because we have hide the toolbar.
This technique does work in some case but user has still a workaround to navigate to previous page. Most of the browser have options of Back in context menu. Thus user can still right click on the page and click Back to go to previous page. We will shortly see the workaround for this issue also.
Following is the code to open webpage in a new window have no toolbar (Back/Next buttons).
window.open ("http://viralpatel.net/blogs/",
"mywindow","status=1,toolbar=0");
Also it is possible to disable the right click on any webpage using Javascript. Add following code in the webpage.
<body oncontextmenu="return false;">
Disable Back functionality using history.forward
This is another technique to disable the back functionality in any webpage. We can disable the back navigation by adding following code in the webpage. Now the catch here is that you have to add this code in all the pages where you want to avoid user to get back from previous page. For example user follows the navigation page1 -> page2. And you want to stop user from page2 to go back to page1. In this case all following code in page1.
<SCRIPT type="text/javascript">
window.history.forward();
function noBack() { window.history.forward(); }
</SCRIPT>
</HEAD>
<BODY onload="noBack();"
onpageshow="if (event.persisted) noBack();" onunload="">
The above code will trigger history.forward event for page1. Thus if user presses Back button on page2, he will be sent to page1. But the history.forward code on page1 pushes the user back to page2. Thus user will not be able to go back from page1.
Warn User if Back is Pressed
You may want to warn user if Back button is pressed. This works in most of the cases. If you have some unsaved form data, you might want to trigger a warning message to user if Back button is pressed.
Following Javascript snippet will add a warning message in case Back button is pressed:
window.onbeforeunload = function() { return "You work will be lost.";
Reference
just use
window.onbeforeunload = function() { window.history.forward(1); };
or if u like to warning user
window.onbeforeunload = function() { return "Back button is not available!"; window.history.forward(1); };

Javascript alert() in a pop up window

I have an anchor tag whose click event calls a JavaScript function, which opens a pop up window.
Now when the user clicks the close button of the pop up window I want to generate an alert(), which asks whether the user wants to close the pop up window or not
Using the below code the alert is generate in the parent window rather than the pop up
<script>
function openForm()
{
var new_window = window.open('file:///C:/Users/Tejora/Desktop/test.html','popup''width=825,height=585,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=0');
new_window.onbeforeunload = function(){
alert("Are you sure you want to close the window"); }
}
</script>
Click
Thanks in advance...
When a string is returned from the onbeforeunload handler, the browser will display a confirmation dialog with that message. Replace this line:
alert("Are you sure you want to close the window");
With this:
return "Are you sure you want to close the window?";
Here is the relevant documentation.

Preempt moving to another page with a pop-up

Let's say I have an update page in HTML5 and if the user edited a field, I want to show a pop-up whenever the user will exit the page (by clicking on another link) without clicking the update button. The pop-up will confirm if the user wants to save his edits or if the user declined, simply continue on the link he clicked before. How to achieve this in HTML5/JavaScript? Is there any function that preempts redirects? Thanks.
Use the onbeforeunload event.
Check out the demo: onbeforeunload Demo
JS code:
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
</script>

How to run a script when clicking on a link?

So, I have a javascript for a popunder ad, (yeah I know they're annoying but it's not an ad, it's a page that opens behind the main page) anyway, because the script has the page open automatically when you go to my page, pop up blockers automatically block it, so what I would like to do is have the script run when someone clicks a link on my page, therefore it is user initiated and not automatically, so it doesn't get blocked by a popup blocker.. basically, so when someone clicks on one of the links on my page, it opens the link, but also opens the pop under behind my page.. I'm kind of new to javascript, but any help is appreciated!
This would be one approach:
HTML:
<a id="link">Link</a>
JavaScript:
function script() {
alert("I'm the ad");
};
document.getElementById('link').onclick = function () {
script();
};
For demonstration see this Fiddle.
/Edit: Sure, here is the JavaScript:
// copy and paste the script from the website
document.getElementById('open').onclick = function () {
load_pop_power();
};
If you don't want to show the ad when the user visits the site, you could however delete half of the code.
You could do something like this
function doSomething() {
//do your actions here
window.location="http://www.gotothelink.com";
}
</script>
click me
When the user clicks the link, the javascript code in the "onlick" attribute is executed. "window.open" opens a new window and "return true" has the effekt that the normal behaviour of the link keeps working.
tiscover
So you need to bind the popunder to a click event on a link
see http://www.pagecolumn.com/javascript/bind_event_in_js_object.htm

Supress/Bypass IE alert message on closing the browser

I have a web page which has a ajax modal popup being loaded inside it.When the modal popup is there we need to restrict the user from closing the browser.we have handled the onbeforeunload event but it showed a popup "are you sure you want ot leave this page".How to supress this popup and stop the page from closing.
<script>
window.onbeforeunload=function() {
window.alert("Hai");return false;
}
</script>
Unfortunately you can never prevent a browser window from being closed as that would lead to all kinds of abuse (imagine a porn site opening a window you cannot close).
The onbeforeunload eventhandler should return a string that could explain to the user what will happen if he closes the window, as the returned string is shown in the confirmation box. E.g:
window.onbeforeunload = function() {
return "If you close the window your unsaved changes will be lost!";
}
That string combined with the non-overridable confirmation dialog with OK and Cancel buttons will hopefully steer the user into making the right decision.

Categories