Browser close event For Firefox not working - javascript

In my application i have to alert user to signout on browser close.
For that i have used the javascript as below
<body scroll="no" onbeforeunload="browerClose()">
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://localhost:8086/egs/ervlet?pri=logOut";
}
this is Working for IE ,but not works for FireFox,
Wats the Problem....Any Suggesstions
Thankxx in Advance,

I would suggest you move the javascript function to the head section of the HTML document. That way it is working for Firefox. Maybe this is because HTML documents are processed in sequential order and you need to define the function before you can use it.
Do something like this:
<head>
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://google.com";
}
</script>
</head>
<body scroll="no" onbeforeunload="browerClose()">
<!-- you code here -->

onbeforeunload event will not work from fire fox version 26 if u used any custom alert message in your application which means you need to perform x operation when closing browser/tab or refreshing page but you put alert message in function which will be called from onbeforeunload then the x (update) operation will not happened.
<html>
<head>
<script>
function unloadfunction() {
alert('test');
// update employee ID in Database
}
</script>
</head>
<body onbeforeunload="unloadfunction();">
</body>
</html>

Related

opener.document.location.href not work in Chrome

Sorry to ask this again.
I am trying to reload the parent window after closing the son window. The code works fine in IE. but not in Chrome (Version 54.0.2840.99 m).
Is it my Chrome setting problem? Or the code problem?
I searched and tried all the solutions but it still not work. Please assist!! Thanks
<!-- Parent Window -->
<head>
<script language="javascript" type="text/javascript">
function popitup2(url) {
newwindow=window.open(url,'email','top=200,left=500,height=500,width=600');
if (window.focus) {newwindow.focus()}
return false;
}
</script>
</head>
<body >
Call S Method 1
<BR><BR>
<a href="#" onClick="window.open('s.htm', '_blank')">Call S method 2/a>
<p id="demo"></p>
<script>
var d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
<!--Son window (s.htm) -->
<head>
<script type="text/javascript">
window.onunload = unloadPage;
function unloadPage()
{
window.opener.location.reload();
}
</script>
</head>
<body>
Son !!
</body>
</html>
It's not the window.opener.location.reload() that doesn't work, but it's calling it from the onunload event. There are very strict rules as to what you are allowed to do in that event handler (to prevent spamming and such)
Check this plunker to see what I mean:
https://plnkr.co/edit/RGIrrupeqxd2bmc9M7ex?p=preview
In the example above you can reload the parent page from the child when clicking the link, but the exact same code doesn't work in the unload event.

Get Back Button to work on parent page when iframe is involved

I am working on a legacy app that has an iframe involved. The back button is working on the iframe and I need it to bypass the iframe and work on the parent window only.
Here is a dumbed down version of the issue and description of what I know.
the main page "index.html" has an iframe that is being added via javascript. It loads a.html, makes an ajax call that then does a window.location = "b.html" At this point if you use the back button it essentiallys makes the iframe go back to a.html and then redirects to b.html so you are effectively stuck on the page. If I remove the ajax call and do an window.location on load everything works ok. However given the architecture and what happen on the page I can't remove the Ajax call from the picture.
Here is the code I am looking at, let me know your thoughts on how to solve this issue. Also I should mention in Chrome 41 this isn't an issue, however the newer chrome 48 and 49 it is an issue. I tried history.replaceState but wasn't able to figure out a way to use it in this situation that made things work.
index.html
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body>
hello world!
<div id="iframeContainer"></div>
<script>
$(function () {
var newIframe = document.createElement('iframe');
newIframe.src = "a.html";
newIframe.id = "A";
document.getElementById("iframeContainer").appendChild(newIframe);
});
</script>
</body>
</html>
a.html
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body style="background-color:#F00;">
<script>
$(function(){
$.ajax({
url:"b.html",
complete:function(){
window.location="b.html";
}
});
});
</script>
</body>
</html>
b.html
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body style="background-color:#00F;">
<script>
$(function(){
})
</script>
</body>
</html>
This is only possible in HTML5 compatible browsers, and it would go something like this..
This goes in the child frame..
// catch the back button click.
window.onpopstate = function(event) {
// make the parent window go back
top.history.back();
};
This also only works if both frames are in teh same domain.

How to disable back and front button of browsers?

I want to disable back and front button of browsers. I have tried with the following code and Iam using onload for div tag and div tag id is order.
<script type="text/javascript">
$(document).ready(function() {
var back= noback();
$("#order").load("back");
function noback() {
window.history.forward(-1);
alert('backbtn');
}
setTimeout("noback()", 0);
alert('loading');
});
</script>
When i tested, alert backbtn is working fine but i can navigate to back page. Please help me how to make disable those buttons.
That would not be a good approach to disable the browser back button rather you should add code to handle the functionality as to what happens when the user clicks on back button.
On approach could be to place this script in the head section of the page so as to dont allow the user to visit the page again
<script>
function restrictback(){window.history.forward();}
setTimeout("restrictback()", 0);
window.onunload=function(){null};
</script>
<SCRIPT type="text/javascript">
window.history.forward();
function noBack() { window.history.forward(); }
</SCRIPT>
</HEAD>
<BODY onload="noBack();"
onpageshow="if (event.persisted) noBack();" onunload="">

How to cancel browser close window?

Is it possible to cancel the browser close window event? Or at least add a message prompting the user whether they are sure they want to leave the page? I tried doing this with the beforeunload event, but it still closes the browser. I am looking for something similar to Microsoft's exchange website.
Thanks!
Here is an Html Code:
<html>
<head>
<script language="javascript" type="text/javascript">
window.onbeforeunload = askConfirm;
function askConfirm(){
return "You have unsaved changes.";
}
</script>
</head>
<body>
</body>
</html>
Paste this code into the text area on this page: http://www.htmlpreview.richiebrownlee.com press the button, then exit out of the page that comes up..
a ha!
It appears there's a supported JQuery solution
$(window).unload(function()
{
//...
});
http://www.webhostingtalk.com/showthread.php?t=927093
this should be cross browser supported...hopefully....
window.onbeforeunload
Will work on Safari, IE, Firefox, Chrome, but not Opera.
onbeforeunload should work fine. Bind this on body/window attribute of the html. For more details check this detailed blog post, blog
use onbeforeunload event http://msdn.microsoft.com/en-us/library/ms536907%28VS.85%29.aspx
<HTML>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</body>
</html>

How to close the window with Javascript

Haven't been programming in JS for a while.
Now, I have following thing:
<html>
<head>
<script language="JavaScript">
function enlarge()
{
window.close();
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onClick="enlarge()" />
</body>
</html>
(it's very simplified, as, in fact, I have WordPress platform with custom JS modifications etc, but in general, this is the idea).
I don't understand why it's not working.
JavaScript cant close the window unless it opened the window.
In your function, replace window.close() with alert('here') and you'll see the function works fine.
If you want your function to close a window, first open one:
<html>
<head>
<script type="text/javascript">
var popup;
function closewin()
{
popup.close();
}
function openwin()
{
popup = window.open('http://www.google.com');
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onclick="openwin()" /> Click to open, then come back here
<br><br>
<img src="addresstomyimg.png" onclick="closewin()" /> Click to close
</body>
</html>
Demo: http://jsfiddle.net/AlienWebguy/pbFha/
If you try running this script using Firefox and use the Firefox's Error Console to look for errors, you can see that the following error gets logged when you run this script.
Scripts may not close windows that were not opened by script.
You can launch the Error Console in Firefox by pressing Ctrl + Shift + J.
On Chrome, your script successfully closes the tab in which it is running.
Note that the right way to use the <script> tag while writing JavaScript is as follows:
<script type="text/javascript">
If you're trying to close the main window, it won't work.
You can only close windows that were opened by JavaScript.
This code works on IE.
<html>
<head>
<script language="JavaScript">
function enlarge()
{
self.close();
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onClick="enlarge()" />
</body>
</html>

Categories

Resources