How to Get the Destination Url for the Onbeforeunload Event

How can I get the destination URL for the onbeforeunload event?

Because it can't be done. The new location is private/sensitive information.
Nobody wants you to know which sites they visit when they leave your site.

How to capture the destination url in onbeforeunload event

Is it possible to know which url they are supposed to jump to at the onbeforeunload event?

No, definitely not. The onbeforeunload event tells you only that the page is about to be unloaded, but not why.

How to extract target hostname during onbeforeunload event

There are two problems in your script:

window.onbeforeunload = function (e) {
var targetHost = new URL(e.target.URL).hostname;
if (targetHost != window.location.host) {
return "Project is unsaved!!!";
else
return null;
};

The first is that you did not close your if. The second is that e.target.URL is apparently not supported by IE. You need to have an A and a B plan.

A plan: Applies to the case when e.target.URL exists. In this case, you handle the case as you initially implemented, but with the syntax fix mentioned above.

B plan: Applies to the case when e.target.URL does not exist. For this case, you should assume that the user leaves the site. You can improve the approach by adding knowledge when the user uses your control to navigate, for instance, you know that a given anchor will stay on the site. In this case, you can define a click event and set a boolean value to true (and the default value of that boolean should be false...), so that later your onbeforeunload event will know that your user stayed on the site even if he or she uses IE.

So you need a logic like this:

var targetHost = !!e.target.URL ? (new URL(e.target.URL).hostname) : ((myFlag) ? (window.location.hostname) : ("elsewhere"));

Get newly requested url onbeforeunload event in javascript

Not as such.

The user could be leaving the page by using a bookmark or typing a new address into the address bar. That is private information and you have no business reading it.

If the user is leaving the page by some action that your site triggered (such as clicking a link) then you could use JavaScript in response to the trigger (e.g. click event on the a element) to set a variable, then read that variable back in your onbeforeunload handler.

How can I get the destination url of a link on the web page in the Javascript onbeforeunload event?

This is what I came up with to handle this and it is working for me.

Detects when user clicks on a page link then evaluates link to determine how to handle appropriately. It does not work for links typed in the browser address bar.

I use jquery magnific-popup (http://dimsemenov.com/plugins/magnific-popup/) to create a popup window (.popupForm-handleExitRequest in my code) that offers the user the option of opening link in same window (and losing their order data), in new window or returning to order form.

$('body a').click(function(e) {     
//if link is reference to page element
if ($(this).attr('href').charAt(0)=="#") {
return;
}

//check if link is to same window
var pathname = window.location.pathname;
var pathname2 = $(this).attr('href');
pathname2 = pathname2.replace(/^.+\.\//, '');
if (pathname.indexOf(pathname2) >= 0) {
//link clicked is contained on same page
//prevent page from getting reloaded & losing data
e.preventDefault();
e.stopImmediatePropagation();
e.stopPropagation();
return;
}

//link clicked on is another page
if (hasMerchandise) { //var to indicate user has items on order form
//give user options: leave page, open link in other page, stay, etc.
// $('.popupForm-handleExitRequest').click(); //roll your own code

//prevent page from getting reloaded & losing data
//in case user wants to cancel page change or open link in another window
e.preventDefault();
e.stopImmediatePropagation();
e.stopPropagation();
} else {
//load new page
$(this).removeAttr('target');
}
});

When window.onbeforeunload is called, can I know where the page is being redirected to?

You would need to add an event handler to Href links in your webpage,
which set a variable on onclick = their href, you can read that variable in your onbeforeunload script. example code would be like this :

  var storeHref = null;
$("a").bind("click", function(){
storeHref = this.href;
return true;
});

$(window).bind("beforeunload",function(){
if(storeHref has mailTo:)
{
//do nothing
}
else
{
//my old logic applies here
}
});

However if the redirection is happening due to some other event,like user entering a new url in the browser, or using a script, you can not detect the destination due to security reasons.

Similar:
How can i get the destination url in javascript onbeforeunload event?



Related Topics



Leave a reply



Submit