dimanche 22 novembre 2020

How to get URL in the browser's address bar that end users recently enter?

End-user can copy a URL of a view in my website then paste it onto the browser's address bar and hit enter to navigate to it. And I have a problem if that URL contains more than one question mark ?.

For example, users can enter this URL: https://mysite.local/testsite/NavigateToView.aspx?navigateURL=https://mysite.local/testsite/myview.aspx?viewID=A123&serviceID=B321

When the server receives this request, it does not understand the URL because the .NET framework allows only one question mark in a URL. I already found a solution to fix this one, which I need to encode the ReturnUrl's value and my server can understand the given URL.

But, I am facing a new scenario that when users copy and paste onto the address bar then hit enter to navigate to it, I am expecting the client should catch that event to modify the recently entered URL before the request sent to the server.

I tried this snippet code, but I cannot get the URL from the address bar properly.

My scenario:

  1. Chrome is currently showing a view of my website https://mysite.local/testsite/home.aspx
  2. Then, I copy and paste the above URL then hit enter https://mysite.local/testsite/NavigateToView.aspx?navigateURL=https://mysite.local/testsite/myview.aspx?viewID=A123&serviceID=B321
  3. In Chrome developer, I can catch the change in the beforeunload event, and please note that this newly entered URL is updated on the address bar successfully. But window.location.href, at this time, is still pointing to the previous URL https://mysite.local/testsite/home.aspx
window.addEventListener('beforeunload', function (e) {
     var pathname = window.location.href;
     //pathname =  https://mysite.local/testsite/home.aspx
});

Is there a way that can solve my case? Thanks!




Aucun commentaire:

Enregistrer un commentaire