I want to detect the exit event on all browser, I tried this code :
<html>
<head>
<script type="text/javascript">
window.onbeforeunload = function (event) {
var message = 'Important: Please click on \'Save\' button to leave this page.';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
};
</script>
</head>
<body>
<p>
Exit your browser ....
</p>
</body>
</html>
this code works in Chrome and safari but it it does'nt work in IE 11 and Fireforx 50.0.1 ... Any ideas ?
From the MDN page following notes are being mentioned
Note also, that various mobile browsers ignore the result of the event (that is, they do not ask the user for confirmation). Firefox has a hidden preference in about:config to do the same. In essence this means the user always confirms that the document may be unloaded.
The hidden keys inside about:config can be found with the keys dom.disable_beforeunload and dom.require_user_interaction_for_beforeunload.
Your code seems fine for the rest, so it may help to look in your config files (The note mentions mobile browsers, however I have these settings on my Desktop browser as well)
Try with
window.addEventListener('beforeunload', function(evt){ ...
this is the response that I find, It work for me.
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome
});
thank you
Related
I have this script in my aspx page
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = function(e) {
return 'Bye now!';
};
</script>
But this is not invoked when I close the browser. I need to pop an alert asking the user to confirm. I need to do this in the aspx page using javascript. The browser I tried was mozilla firefox. Thanks
The following code should fix your issue:
(function () {
window.unloader = function(e) {
(e || window.event).returnValue = null;
return null;
};
window.addEventListener("beforeunload", window.unloader);
})();
You are not going to be able to specify a message, it is defaulted by the browser.
I have this code that alert when I click a link or refresh or close tab.
But I need alert only on close window (tab). how to do this? I have many external and internal links on my site.
<html>
<head>
<script type="text/javascript">
window.onbeforeunload = function (e) {
var e = e || window.event;
//IE & Firefox
if (e) {
e.returnValue = 'Are you sure?';
}
// For Safari
return 'Are you sure?';
};
</script>
</head>
<body>
<!-- this will ask for confirmation: -->
<!-- I need to alert for external links. -->
external link
<!-- this will ask for confirmation: -->
<!-- I don't need to alert for local links in my web site -->
internal link
</body>
</html>
document.activeElement is handy in this scenario, it will be equal to the link you click on to unload the page. You can then check the link's href attribute whether it contains your host name. An example for codepen.io would be (demo here):
window.onbeforeunload = function (e) {
var e = e || window.event;
var element = document.activeElement;
if(element.tagName === 'A' && element.href.indexOf('codepen.io/') === -1) {
//IE & Firefox
if (e) {
e.returnValue = 'Are you sure?';
}
// For Safari
return 'Are you sure?';
}
};
My initial thought was to do a regex for http:// and https:// to see if the path is relative but it looks like browsers basically convert relative paths to absolute and prepend the http...
If you want to write this code so that it is more universal you can use location.hostname instead of statically typing the hostname to do a comparison on.
Finally, your milage may vary in IE depending on which IEs you want to support the above code might need updated. The trend nowadays is to support IE11+ :)
I found an answer, but it was for JQuery. Here is the link:
http://jquerybyexample.blogspot.com/2010/12/disable-cut-copy-and-paste-function-for.html
: I want something in plain Javascript which work on chrome, latest firefox, safari, and IE 8 and 9.
Update
Due to all the negative comments saying that this is a bad idea for an internet site I can only say "I agree". Please note that this is for an "intranet" application where cut, copy, and paste need to be overidden as the default browser behaviour for cut copy and paste needs to be customized to handle embedded tags in a rich text area
of course it is not appropriate to do stuff like this, but that was not #Zubairs question, so i think voting down is not correct here, as he made his point clear.
now to the question: if jQuery can do it, native javascript can do it of course too.
you must prevent the cut, copy and paste events:
document.body.oncopy = function() { return false; }
document.body.oncut = function() { return false; }
document.body.onpaste = function() { return false; }
this prevents the right-click-context-menu, this is not needed if you use the 3 other event-handlers but just to let you know ;-)
document.body.oncontextmenu = function() { return false; }
IMPORTANT: the body must be loaded (of course), document.body because IE needs it (document.oncopy will only work in chrome/firefox/safari)
a good way
var D=document.getElementById('b4');
if(D.addEventListener){
D.addEventListener('paste',function(e){false;e.preventDefault();},false);}
else{
D.attachEvent('onpaste',function(){return false;});}
warning : code must be under html target/s ,
just before the close body tag for example
Edit: adding this to a body tag seems to work on all of my test browsers including the Opera, Chrome, Seamonkey (so I assume Firefox) and IE9
<body oncopy='return false' oncut='return false' onpaste='return false'>
you can put them in other tags if you want to allow some functions in some places and not in others
You can catch a [Ctrl]+[C] keypress:
addEventListener("keydown", function(e){
evt = (e) ? e : window.event; // Some cross-browser compatibility.
if(evt.ctrlKey && evt.which == 67){ // [x] == 88; [c] == 67; [v] == 86;
console.log("Ctrl+C pressed!");
evt.preventDefault(); // Cancel the copy-ing function for the client.
// Manual Copy / Paste / Cut code here.
}
});
Working snippet
oncopy="return false" oncut="return false" onpaste="return false"
This code will be prevent cut, copy and paste of a website.
Working Snippet
I need to stop all the page redirection through javascript.
I have some script which will redirect the page to some other location.
How can I stop all the page redirection on my page through jquery or javascript.
You can stop the redirect by doing the following.
<script language="JavaScript" type="text/javascript">
//<![CDATA[
window.onbeforeunload = function(){
return 'Are you sure you want to leave?';
};
//]]>
</script>
Not sure if all browsers support this but that should do the trick.
If I'm understanding you correctly, you need to stop users navigating away from the page?
If so, Marcos Placona's answer is one part of it - although all what you actually need is:
$("a").click(function(e) { e.preventDefault(); });
You also don't want people to hit F5 I'm guessing? Well that's harder. Preventing defaults on key press, cross-browser is horrible. But a couple of codes that work in some browser are below here:
function disableF5() { // IE
document.onkeydown = function() {
if (window.event && window.event.keyCode == 116) { // Capture and remap F5
window.event.keyCode = 505;
}
if (window.event && window.event.keyCode == 505) { // New action for F5
return false; // Must return false or the browser will refresh anyway
}
}
}
function disableF5v2() { // FIREFOX
$(this).keypress(function(e) {
if (e.keyCode == 116) {
e.preventDefault();
return false;
}
});
}
However, the cross-browser issue can partly be solved with -
window.onbeforeunload = function(e) {
var message = "Your confirmation message goes here.", e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = message;
}
// For Safari
return message;
};
Hope that all helps anyway. The last bit of code is from another question on this site here.
Rob
Are you trying to stop an iframe breaker? Something like:
<script language="JavaScript" type="text/javascript">
<!--
function breakout_of_frame()
{
if (top.location != location) {
top.location.href = document.location.href ;
}
}
-->
</script>
If so, you can not.
If it's only redirects through link clicks, in jQuery you can do this:
$(".someClass a").unbind('click');
if it's an existing page redirect, you'll need to give us some more information about what kind of redirect this is, so we can help further.
Hope this helps you
AFAIK this won't be possible, since page redirection can occur in many ways. He can click on an anchor tag, can submit a form with different action, set window.location to another page on button click and invoke server code to redirection.
And you won't be able to detect many of these.
I'm trying to launch a popup window from a Javascript function and ensure it has focus using the following call:
window.open(popupUrl, popupName, "...").focus();
It works in every other browser, but IE8 leaves the new window in the background with the flashing orange taskbar notification. Apparently this is a feature of IE8:
http://msdn.microsoft.com/en-us/library/ms536425(VS.85).aspx
It says that I should be able to focus the window by making a focus() call originating from the new page, but that doesn't seem to work either. I've tried inserting window.focus() in script tags in the page and the body's onload but it has no effect. Is there something I'm missing about making a focus() call as the page loads, or another way to launch a popup that IE8 won't hide?
The IE8 is not allowing this feature because of security issues
Windows Internet Explorer 8 and later. The focus method no longer brings child windows (such as those created with the open method) to the foreground. Child windows now request focus from the user, usually by flashing the title bar. To directly bring the window to the foreground, add script to the child window that calls the focus method of its window object
http://msdn.microsoft.com/en-us/library/ms536425%28VS.85%29.aspx
You might try this. Not sure if it will work though>
var isIE = (navigator.appName == "Microsoft Internet Explorer");
var hasFocus = true;
var active_element;
function setFocusEvents() {
active_element = document.activeElement;
if (isIE) {
document.onfocusout = function() { onWindowBlur(); }
document.onfocusin = function() { onWindowFocus(); }
} else {
window.onblur = function() { onWindowBlur(); }
window.onfocus = function() { onWindowFocus(); }
}
}
function onWindowFocus() {
hasFocus = true;
}
function onWindowBlur() {
if (active_element != document.activeElement) {
active_element = document.activeElement;
return;
}
hasFocus = false;
}
Yeah I can't test this on IE8 at the moment either but have a play with this document.ready method instead of the body.onload:
test1.html:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
function openNewWindow()
{
window.open("test2.html", null, "height=200, width=200");
}
</script>
</head>
<body>
<a onclick="openNewWindow()">Open</a>
</body>
</html>
test2.html:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ window.focus(); });
</script>
</head>
<body>
<div id="container" style="background:blue;height:200px;width:300px">
</div>
</body>
</html>
I figured out what the issue was - turns out the reason running window.focus() in the onload wasn't working was because the first window.open().focus() call caused it to start flashing in the background, and after that any subsequent focus calls wouldn't work. If I don't try to focus it from the calling window but only from the popup it comes to the front normally. What an annoying "feature"...
The problem is the Window.focus method does not work in Internet Explorer 8 (IE 8). It's not a pop up blocker or any settings in IE 8 or above; it's due to some security I believe to stop annoying pop-ups being brought back up to the top.
after a lot of hair pulling and googling i found the following:
Microsoft suggest updates but this doesn't appear to work plus how do they seriously expect me to ask all of the users my site to update their machines!
so I've come up with this work around or fix.
What i do with the window is:
first I check if the window is open
if it's open, close it
open a new fresh version of the window on top.
javascript code to include at header or in separate file:
function nameoflink()
{
var nameofwindow = window.open('pagetolinkto.htm','nameofwindow','menubar=1,resizable=1,width=350,height=250');
if (nameofwindow) {
nameofwindow.close();
}
window.open('pagetolinkto.htm','nameofwindow,'menubar=1,resizable=1,width=350,height=250');
return false;
}
link on the page:
Click Here to go to name of link
Tested in MS Windows 7 with IE8 not sure of exact version.