How can i get the previous page domain without using document.referrer - javascript

When I use document.referrer to get info of the previous page, sometimes it returns null, I wonder is any other method to get the domain of the previous page?
This is for front-end, ReactJs

You may try either of this:
window.history.back();
window.history.go(-1);
In the second example, go(-1) reads as "Go 1 step back in history". You could use go(-2) to go back 2 steps and so on

Related

How does JavaScript object history stores an URL into its stack?

I have this history logs in the window.history object with 5 as my current page.
5) http://myapp.com/great-new-story/01242020.html
4) http://myapp.com/great-new-story/01052020.html
3) http://myapp.com/great-new-story/12012019.html
2) http://myapp.com/allnews.html
1) http://myapp.com
Then I want to go to page allnews.html (or no.2) , will the allnews.html be logged in No. 6?
Or it will not be logged and will use (no. 2) since allnews was already in the stack?
Here is a simple experiment:
Count the length of the history.
window.history.length(); //4
Go some steps back
window.history.go(-2);
Get the length again. Its still 4!
Answer:
Navigating the history would not increase the depth.
AFAIK it will push in a new one, because of the simple fact, this is also used for the back and forward navigation buttons, so it should be like that, also I just checked it.
Created a new tab > open google.com
searched something and basically it redirected, so then window.history length increased,
Then In omnibar . search bar I wrote google.com -- the length of window.history incremented once again.
And as I said, it should be that for back and forward stack maintenance
Thank you for the help guys, really appreciate it :y: :)
However, I have found the answer to my problem.
And it's about using the location.replace , instead of using location.assign when redirecting to a site.
I have found out that location.replace -
removes the URL of the current document from the document history,
meaning that it is not possible to use the "back" button to navigate
back to the original document.
as per w3schools. That's why I started to use Location.assign , where after I click the link, then click the history.back , it redirected to the correct previous site.

Get previous site url in a Single Page Application using javascript

I'm working on a vue single page application.
Situation: I have a page which displays 1..* results (Page A), if there is only 1 result available, then this result is automatically selected; meaning the user is redirected to the items detail page (Page B).
[Page A] <-> [Page B]
Problem: if the user is on Page B and navigates back then the 'auto-select'-clause is triggered again which results in a redirect again to Page B. This means you cannot navigate back once you've entered Page B - this is unacceptable.
Thoughts: The best solution I can think of is to add a check to the auto-select clause which matches the origin (where the user came from, e.g. Page B) to the desired destination (where to user is redirected to, e.g. also Page B), and if they do match, do not trigger the redirect. The problem is that I have no way of knowing where the user was before.
I tried to use document.referrer but this isn't suitable for a single page application.
I tried to work with vue-router properties, but none of which brought me any futher.
The only other solution I can think of is to store each navigation in a cookie (or similar) to match against, but since this is quite a rare case I try to avoid having too much of an overhead.
Any idea? Is there something similar like document.referrer but for single page applications? Or is there a way to extract information from the history?
When result is only 1, then PageA will redirected to PageB ,if in this case , let the route like "/PageB?only=1".
in pageB, if the query's only is 1, then when you click the back button,redirected the page that you want to back.
I am not sure it is the best~ but it should work。

How to Replace the path name of URL using JavaScript/jQuery?

I am trying to replace the url and load the page.
Example:
URL: http://www.example.com/account/edit
I want to replace this as http://www.example.com/account/add. And load this page.
For that, I tried by using location.pathname.
location.pathname = "/account/add";
When using like above, the add page is displayed. But after that, URL http://www.example.com/account/add is loading. Then the add page is not displayed. I get that page is not found message.
The host name should vary based on running the project. So I want to resolve this without based on host name.
How can I achieve it?
If you want to retain the page in your session history (meaning you want to be able to use the back button to it) use the assign method. Otherwise, you replace.
Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
http://mdn.beonex.com/en/DOM/window.location.html
window.location.href = "http://www.example.com/account/add";
OR
window.location.replace("http://www.example.com/account/add");
This will redirect to the new page.
Actually you can change your url some other ways like below:
window.location.assign("http://www.mozilla.org"); // or
window.location = "http://www.mozilla.org";

Back button jquery to previous page after current page has been reloaded

I need a way to go back to a previous page after the current page has been reloaded.
At the moment if I use my simple history -1 on the page that has been reloaded in jQuery it will send me back to the same (reloaded)page as is only expected.
Is there a way to identify that the page is the same page and therefore ignore it as a relevant option to go back to?
Here is the code I am using at the moment:
$(document).ready(function(){
$('a.back-link').click(function(){
parent.history.back();
return false;
});
});
Thanks,
Aidan
You could always use document.referrer to get the address of the page that originally referred you to your current page (location.reload() should not affect the referrer variable)
location.href = document.referrer;
A working example can be found here http://iamdevelop.in/referrer
Using your history.go(-1), you can simply specify a different number to go a different number of pages.
Try using:
window.history.go(-2);
This should then skip that 1 back page which is the same and go back to the page before that.
Source

Update URL parameter with javascript, without ruining the history?

I'm looking for a way to update the url in the status bar..
I have a gallery, and when you click your way through the gallery I want the image ID to show up in the URL, so that the user can link directly to the image..
I've read about using hash and so. but as far as I've tried it, that "ruins" the history.
If I click the back-button in my browser the previous image would be shown.
Is it possible to add or update a URL parameter, without ruining the history?
Thanks in advance
Use location.replace to replace the current location:
Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
Do it simply this way, when switching to images, add a hash to the url, for example:
location+='#image-'+image_id
your location will become
http://example.org/images/#image-3
instead of the initial
http://example.org/images/
and onload, check if location.hash is not empty, and matches with ^image-(\d+)$ (regular expression pattern), if it matches, do the usual thing you'd have done if a user clicks on image with id (\d+).
To preserve history, use reallysimplehistory.

Categories

Resources