Do javascript after redirect - javascript

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.

Related

Modify current URL (Gatsby / Reach Router)

Question: Is it possible to modify the current URL that's shown in browser's address bar and browser history?
To be specific, I only want to modify the URL that is visible to the user; I don't want to trigger navigation. (I have a Gatsby app, and Gatsby is using Reach Router.)
Motivation: I have a gallery of images that the user can click and navigate to URL such as /images/?id=52. The advantage of this approach is that /images/ can be prefetched to enable instant rendering of the page. However, this scheme is unfriendly to users who have disabled JS, as they will see no images at all when they navigate with query parameters. So I have also prerendered pages like /images/52/ that work without JS. So what I want to do is navigate the JS users with query parameters, but then modify the URL that they see to a URL that can be shared with anyone including non JS users.
What I think you're looking for is either window.location.replace() or window.location.assign()
Replace is merely visual, so if the user were to copy the URL to share with their friends you can manipulate that url that they see and copy.
Assign loads a new document, as if the URL you passed it is the one that got the document.
https://developer.mozilla.org/en-US/docs/Web/API/Location/replace
https://developer.mozilla.org/en-US/docs/Web/API/Location/assign
Edit: a comparison
Difference between window.location.assign() and window.location.replace()
I believe you'll need to create a NodeJs server to handle this sort of action. It can take a query parameter from the URL ('images/?id=52') and then return the user to the URL 'images/52'.
Or you may be able to use the 'gatsby-source-filesystem' package.

Setting window.location vs. typing url?

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')

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.

How to pass parameters in a url without page refresh?

I want to pass different parameters on my webpage without page refresh. Is this possible by any means?
For example:
My current webpage is "currentpage.php". After interact with some form elements -using jquery/ajax or any other- the url becomes "currentpage.php?x=foo&y=bar"
So that I can use $_REQUEST['foo'] or $_REQUEST['bar'] for some work on my currentpage.
If there's no page refresh, then no request to server. But behavior you are describing is similar to SPA. Sample here.
If you want to do so, use sessions and store your variables in
$_SESSION['foo1']=$_REQUEST['foo'];
In this manner you may be able to save your data and then pass parameters using javascript with window.location.href or by submitting forms.

Javascript reload to load a new instance and not reload the page

I am using window.location.reload in a jQuery dialog on close. The issue is that if users posted to the page earlier, it will give them a dialog to reload the post data. Is there a way to load the page without "reloading", or "refreshing".
Something similiar to PHP's header("Location");
I need it to load the same url, just not reload it.
I understand that I can use window.location = window.location, but this does not work.
How about window.location.href = document.URL;
In addition to the direct answers, looking at this issue from a broader scope might help.
The user is being prompted to reload post data because they came to that page via a POST method - if you were to employ the PRG pattern, you'd avoid the post data prompt, and reap the various other benefits it provides. That said, it is a bit more work :)

Categories

Resources