javascript open window url on exit - javascript

Hello I am trying to open a new URL on page exit in a browser window using javacript. My goal is when user closes the window to see the javascript alert box and when he press "Leave this Page" Instead ot the browser window to be closed to be redirected to google.com.
my code so far is the following:
<script type="text/javascript">
$(document).ready(function(){
var areYouReallySure = false;
var internalLink = false;
function areYouSure() {
if (!areYouReallySure && !internalLink) {
areYouReallySure = true;
location.href="http://www.google.com"
return "Do you wish to go to google?";
}
}
window.onbeforeunload = areYouSure;
$('a').click(function(){
internalLink = true;
});
});
</script>
Rigth now when I chose leave this page it only closes the window.

This is impossible, and with good reason. Imagine if spam sites controlled what happens when you close their page. As a security measure, the only thing you can do when a user tries to navigate away from your page is display a message. This was implemented mostly so users can be warned that they haven't saved their data.

Related

Detects Redirect and open a modal (Popup)

I have a scenario trying to redirect DNS to another URL and i.e http://oldexample.com to another page of new website https://example.com.com/newpage
I am trying to make a script that detects traffic to https://example.com/newpage from http://oldexample.com and if it's detects that trigger to open modal/popup/alert message will saying some information on this page (https://example.com/newpage).
I have tried following solution but it does not work? Is it right way to implement it?
$(document).ready(function($) {
var isExternal = document.referrer;
console.log(isExternal);
if (isExternal === "https://oldexample.com") {
// Do something
alert('Referred from expected page. Probably.');
}
});

onscroll() to open a new tab without pop-up alert

I need to open a new tab when a user is scrolling in my website.
The problem is, as far as i understand, that in order for a new tab to open automatically without a pop-up warning from the browsers, the user must first trigger an event in the DOM but apparently onscroll() does not count as one.
Is there a way for me to trigger an even when a user is scrolling in my website that will allow me to open a new tab without the pop-up alert from the browser?
This is currently my code:
window.onscroll = function(ev) {
if(clicked === 1) {
return false;
}
clicked = 1;
var win = window.open('<?php echo $redirect ?>', '_blank');
win.focus();
};
That is native browser functionality and its a good one.
Just imagine if every site could pollute your browser with a number of tabs.
For it to work the user has to accept and allow popups from your domain

Detect if new window if opend using javascript

Creating a small chat application where user can start chat by clicking on chat button next to user's profile.
Once anyone will click on chat button a popup window will appear. Now I want to detect dynamically if this popus is still open or closed.
If popup is opened= Display new message alert in notification.
if popup is closed = Do not alert new message in notification (As chat window is already opened)
This is what I tried:
Chat with user 2
Chat with user 1
// on document ready
setInterval(function(){
chkAlerts();
}, 10000);
var newwindow = null;
function popitup(url) {
if ((newwindow == null) || (newwindow.closed)) {
newwindow = window.open(url,'Buy','width=950,height=650,scrollbars=yes,resizable=yes');
newwindow.focus();
}
else {
newwindow.location.href = url;
newwindow.focus();
}
}
function chkAlerts(){
if ((newwindow == null) || (newwindow.closed)) {
//Do something
alert('now window is closed show alerts');
}
}
With this code I am always getting
alert('now window is closed show alerts');
chkAlerts will always show the alert as the initial value of newwindow is null.
Presuming you only want the alert after a chat window has been closed tweak the logic to ignore the initial null state;
function chkAlerts(){
if ((newwindow != null) && (newwindow.closed)) {
//Do something
alert('now window is closed show alerts');
}
}​
Your error is not in the javascript code, is in the anchor, you are closing the double quotes inside the call to the function, they must be like this:
Chat with user 2
Anyway, but also, as you can see, you are only storing the last opened window, if your interest is in know that both are opened you should create newwindow as an array.
Cheers,
Continued from comments:
Just so you know, when reloading or navigating away from the
window-opener page, the reference to newwindow will be lost.
Yes jan, this is the actual problem, do you think is there any
function in javascript to do this... or do i need play around with
session or something?
Not even possible with sessions or any other server sorcery.
Your option if you really need to keep track of the chat window is to include the chat on your "opener" page and make it an inline AJAX chat.
You could have an AJAX listener on all your pages and "open" that chat window on all open tabs/windows of your site, so that the user doesn't lose track of it.

Is it posible to use "confirmit" to open new url in the same window?

I'm using a the confirmit java script function to redirect to a new url with a positive response. It doesn't do exactly what I want - and maybe I can't have exactly what I want.
Here's what it does - onunload, with a positive response it opens the new url in a new window, which can be blocked by a pop-up blocker without the user even knowing, giving the impression that nothing happened. When it does open the new URL it opens in a new window making the desktop crowded. Its just not a great solution.
What I want it to do - onunload I'd like it to open the new url in the same window and have it not be classified as a "pop-up" to be blocked. I tried switching to onbeforeunload.... nuthin.
Is there a simple solution?
here's my current code:
<script language=javascript>
function confirmit()
{
var closeit= confirm("My message");
if (closeit == true)
{window.open("http://MyDestinationURL");}
else
{window.close();} } window.onunload = confirmit
</script>
Instead of opening a new window you can set the location of the current window:
<script type="text/javascript">
function confirmit() {
if (window.confirm("My message")) {
window.location.href = 'http://MyDestinationURL';
} else {
window.close();
}
}
window.onunload = confirmit;
</script>
I am not sure if you actually want the else case where it tries to close the current window. If you just want to stay on the current page on a negative response, remove the else part.

java script+onbefore unload

i m creating a web application, after login user navigates to home page where i have a navbar(Links to pages/url).Navbar is like this
Home-Nvigates to home page
Profile-Navigates to profile page
Chat-navigates to chat page when user clicks on chat link .
I want that when user closes the browser window ,accidentally or manually he should get an alert for that i have used onbeforeunload function like this
<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>
Now when i m closing the browser on chat page i m getting following alert "You have attempted to leave this page." that is working fine,but now the main problem is that when i m clicking on other links like
Profile or Home this function is called and me geting this alert even though i have implemented this code in chat.jsp .Even i m refreshing this page i m getting this alert mentioned above which i should get only when i closes widow of chat.jsp page rather than other pages or Refresh.Plese help
Onbeforeunload fires when the page is unloading be it by refresh, navigate or page close. You could add an onclick eventhandler to anchors on your page that remove your onbeforeunload event to avoid it and you might be able to do the same with an onkeydown event on the window/body/page however I don't believe there's a way to catch users pressing the browser's refresh button.
example:
<html>
<head>
<script type="text/javascript">
function warnUnload(){
return "Your message here...";
}
function linkNavi(){
window.onbeforeunload = null;
}
function isRefresh(e){
var key = null;
if(window.event) // IE
key = e.keyCode;
else if(e.which) // Netscape/Firefox/Opera
key = e.which;
if(key == 116) //F5
window.onbeforeunload = null;
}
window.onbeforeunload = warnUnload;
window.onkeydown = isRefresh;
</script>
</head>
<body>
Google
Home
</body>
</html>

Categories