Javascript window close - javascript

I need to get a custom alert box when user clicks on 'X' (window close).
User is given 'OK' and 'Cancel' button options
OK button closes the window. Cancel button cancels the close action

I think you may find your answer on this post which looks similar - javascript confirm dialog box before close browser window
You have to show what you have done so far

Html code
<html>
<head>
<script type="text/javascript">
window.onbeforeunload = function() {
return "Did you save your stuff?"
}
</script>
</head>
<body>
</body>
</html>

Related

Logout once the Browser is Closed

I'm trying to record user sign out time in mysql DB once he close the browser. I searched for it but the most articles talking about window.onbeforeunload JavaScript event, but doesn't work with all browsers. even it works with F5 button can't specify massage or action,.
<!DOCTYPE html>
<html>
<body onbeforeunload="return myFunction()">
<p>Close this window, press F5 or click on the link below to invoke the onbeforeunload event.</p>
test by redirecting
<script>
function myFunction() {
return "specific massage";
}
</script>
</body>
</html>

Close current window when it call Open Tel:

When I call window.location.href("tel:"+'+0060199999') it will open the Skype which is working fine.But i dont want current browser window stay open, i want to close it.If i put close, it is not even open the Skype.Is this possible to do?
<html>
<script>
window.onload = function () {
window.location.href="tel:+0060199999";
window.close();
}
</script>
<body>
Let me test
</body>
</html>

How to call a function when close popup window

I am calling the Javascript window.open() function to load another url in a pop up window.
When the users closes the popup window, I want the MyThanks() function to be called. How I can do this?
My Script :
<!DOCTYPE html>
<html>
<head>
<script>
function openWin(){
myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
myWindow.focus();
}
function MyThanks(){
alert("Thanks");
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
Regarding usage of popups:
Are you aware that Facebook & Twitter still use popup windows for user like & follow. Go to http://www.ekwebhost.com and click the Facebook "Like" button or the Twitter "Follow" button. Why can't I use popup windows for my own needs?
function openWin(){
myWindow=window.open('http://facebook.com/ekwebhost','','width=600,height=400,left=200');
// Add this event listener; the function will be called when the window closes
myWindow.onbeforeunload = function(){ alert("Thanks");};
myWindow.focus();
}
Try this!
You can swap out the function(){ alert("Thanks");}; for MyThanks to call your function.
The new window that you open will have an attribute called opener that references the Window object that created it. You can then call functions on the parent window by calling window.opener.MyThanks();
Popup:
<script type="text/javascript">
function closePopup() {
window.opener.MyThanks();
window.close();
}
</script>
<div>
<h1>My Popup</h1>
<!-- Form or info to display -->
<button onclick="closePopup();">Close</button>
</div>
While there is nothing inherently wrong with browser popup windows, they can be annoying since the user can lose them behind other windows. Also, it takes their focus off of your page to do something else. The application that I currently develop is trying to move all of our popup windows to Javascript dialogs. It gets pretty annoying for users once you get 3 popups deep.
There are a bunch of ways to create dialogs within your webpage. jQuery UI is one library that I like.
You can try this:
<script>
function openWin() {
myWindow = window.open('http://facebook.com/ekwebhost', '', 'width=600,height=400,left=200');
myWindow.focus();
MyThanks();
}
function MyThanks() {
alert("Thanks");
}
</script>

JQuery - Write to opener window

I have an HTML page that opens another page via JavaScript. When a user clicks a button in the other page, I want to post a message in a DIV of the opening page via JQuery. I cannot put my finger on it, but I cannot seem to get this to work. Here is my opener page
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<body>
<input type="button" onclick="window.open('dialog.html', '_blank', 'height=200, width=300');" value="launch!" />
<div id="testDiv"></div>
</body>
</html>
When the user clicks the "launch!" button, a dialog will appear. The code for the dialog looks like this:
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
</head>
<body>
<input type="button" onclick="updateOpener()" value="Update Opener" />
<script type="text/javascript">
function updateOpener()
{
var testDiv = window.opener.jQuery("#testDiv");
if (testDiv != null) {
alert("here");
testDiv.html("Updated!");
}
}
</script>
</body>
</html>
Surprisingly, the alert box appears. However, I cannot seem to update the HTML of the DIV in my opening page. Does anyone know how to do this?
You're referencing "confirmDiv". Where is that DIV?
You can't do that if the parent page (the opener) resides on another domain. Otherwise, your code works perfectly.
Also, your != null check is probably not doing what you think it is doing, as the jQuery function never returns null. If you are checking for the existence of an element, you need to do it this way...
var el = $("#myElementId");
if(el.length == 0)
alert('Not found!');
Ummm, it works for me in Firefox 3.0.11, IE8 and Chrome 2... (I.e. the dialog.html button updates the HTML in the opener page to say 'Updated!'.)
Oddly, your example works fine for me in Chrome, IE 8 and FireFox. Do you have any other details?

Link in popup window does not close the window?

I have this bit of html code, but when I click the link, it does not close:
<html>
<head>
<title></title>
</head>
<body>
<h2>Members Only</h2>
<p>You must be a member.</p>
<p>Please Sign In or Create an Account.</p>
Close this Window
</body>
</html>
I just removed the spaces and it works well :
Close this Window
Popups suck, use a modal dialog instead, especially for something as simple as a notification.
That being said, try this:
Close this Window

Categories