Setting window.location vs. typing url? - javascript

My main question is in bold at the bottom. I would love an answer to that especially but if you'd like to help me figure out the rest of the problem, please continue reading.
I am working on a web application whose session expiration is being handled by a Spring backend (it's the default Tomcat 30-minute session expiration). If you are logged into the application and then you type in 'www.myapplication.com/portal/logout' (not the real url, obv.) you are logged out and redirected to the login page. Great. However, if you set
window.location = 'http://www.myapplication.com/portal/logout'
in the client-side javascript, that url appears in the url bar in your browser but a whitelabel error page results which is being generated by another service on the backend.
Why is there a difference between typing the url versus setting window.location in the code? Should there be a difference? Or do you think this other service is funking with redirection? If so, why would the same error not occur when you type the url?
PS. I also tried window.location.href = url and window.location.replace(url), to the same effect.

You should try:
window.location.assign(url);

Setting window.location is a common error. The location object has a function called assign that will open a new location.
Try using the following instead:
window.location.assign('http://www.myapplication.com/portal/logout')
You could also use the open() function of the window object.
window.open('http://www.myapplication.com/portal/logout')

Related

Do javascript after redirect

I redirect to a new page with javascript as
window.location.href = new_page;
When the new page loaded, I want to run some javascript functions.
However, how can I detect when a page loads, it is from my above redirect?
I consider two options: creating a session or passing variables via the URL as
window.location.href = new_page+'&redirected=1';
Then checking if redirected query is set on each page.
I wonder if there is a simpler or more standard approach to do so?
Passing parameters throught URL works fine, but it's not a pretty good solution. I suggest you to try local storage, with this solution you even could track from and to urls the user has been redirected.

JavaScript - Determine how a user arrived at html page

I'm building a web application and for one of my pages, I need to determine how a user arrived at the page (i.e. by redirect or by directly typing in the URL).
Is this possible with vanilla JavaScript?
document.referrer.
You can also read Referer header on your backend server, because browser automatically includes it.
You can try using document.referrer
If somone types url in address bar it should return an empty string, otherwise if a link was clicked it should return prior page url

How to open a new window and extract a param after user authenticates

I am making a simple web app and the service provider I am using authenticates through the OAuth 1.0 protocol. I would like to know how to extract a param after user signs in from a separate browser window and grants access. I have done this before in Objective-C for an iOS app using an event, but am unaware how to do it using a browser and using JavaScript. I assume I do something similar using an addEventListener() or onClick() type method? Also, can this quickly be achieved using a JQuery method? Details below.
I want to:
Open a separate window to allow the user to sign in.
User will then 'GRANT' access, at that point, the page will reload with the newly appended parameters to the URL.
I then need to extract the params, which are the verifier and token oauth_verifier=verifier&oauth_token=token, from the URL after user signs in.
Something to note:
The service provider does not allow a callback. The user need to pass oauth_callback=oob for the param.
The link below is what I come across often while searching for an answer, however it just describes how to open a new window and point the user to a URL:
How to open a new window and insert html into it using jQuery?
Thank you for your help!
If you open a new window with JavaScript, as described in the link you found, can you then access the location property to read the URL parameters?
var w = window.open();
console.log(w.location);
Maybe you need to check the value every 10 seconds with a setInterval or something, to get the new URL value once it updates after your user signs in.
Hope this helps!
Do not put any authentication information in the URL as it is not protected and can be intercepted.
You can use localStorage to store authentication information and have that be available between different browser windows.

How to change browser url without changing anything in history

You might think this is a duplicate, but it's not. I've seen the other SO answers about changing the browser url with replaceState() but I have a problem...
On one webpage on my website, it receives a lot of parameters, so the link goes on and on and on... I did replaceState(), and it shortened the URL by a lot. That worked. If I go on another link, and I go back in history, the URL will still be the same that I defined on replaceState() and the page gives a 404 error. Is there a way to just load the link the way it is and then temporarily change the URL so the user doesn't see a huge link but the system uses the actual URL?
I'm using JSP, go ahead and give me answers in JavaScript, JQuery, or Java.
You could store the parameters in localstorage (ie a cookie) then have your next page un-pick them from localstorage, thus reducing the size needed of the URL in the frst place. Example code (stolen from Storing Objects in HTML5 localStorage) :
// add to storage
localStorage.setItem('myAttibute', 'acceptable');
// Retrieve the object from storage (on another page)
var sMyAttibute= localStorage.getItem('myAttibute');
alert("myAttibute=" + sMyAttibute);
Hopefully it'll tell you that my attribute is acceptable.

Check for Redirect url is correct or not?

I am calling window.location.href = "some url";
I want to check whether that url is opening or not. Means if the url is wrong how do check it in javascript so that I can redirect it to another url.
Thanks.
You can't, not really anyway. Once you change the location, the browser will unload your page and begin to load the new one, there's no way to find out where the browser is going or if it failed to reach the target page.
If the URL is on the same domain you could check to see if it returns a status 200 with an AJAX request before setting the location, but if it's on another domain then you're out of luck due to the same origin policy.
You need to use a server side component for this to work.
Expose an ajax service that will perform a HEAD on the given url and return a status.
Mind that this service needs to be protected to avoid being used for DDOS.

Categories

Resources