How can I get the next/forward url with javascript? - javascript

We have function available to get the back/past url using: document.referrer, so is there any way or function to get the "forward" url?
I just want to know if the current page has back or forward links. I have searched lot into the history and history-session but they are very limited due to some security issues.
Is there any way to get the next url?

Unfortunately there is no programmatic way of extracting the forward or reverse history from a browser. This is a security precaution. You can only trigger movement through the history and not view any of the entries.
A similar question...
How do you access browser history?
To confirm W3C states under "5.5.2 The History interface" in the following specification http://dev.w3.org/html5/spec/history.html#the-history-interface
"The actual entries are not accessible from script."
Edit and I would add that this is the only place that the information is available. If you are developing an extension you may be able to track urls yourself by storing the current url in storage otherwise you are out of luck.

You cannot read the history object. There is no way for your script to know which URL the forward button will lead to, or if it is available at all.

Related

Google AMP - Save current page URL to local storage

Can anyone point me in the right direction? I'm building a landing page that is supposed to store the current page URL with query strings to local storage. I've looked all over, but can't find a solution that will keep my AMP page valid.
I know this is a 3 year old question, but what you're trying to accomplish is doable through the use of the amp-script component.
You can read up here:
https://amp.dev/documentation/components/amp-script/
Hope this helps somebody in the future.
Nowadays you can use for custom JS and access the localStorage, but as far as I know, you can't access the page URL directly.
Look at this compatibility table: https://github.com/ampproject/worker-dom/blob/main/web_compat_table.md
Document.location is marked as false.
I'm working in an AMP project that needed the current page URL. What I did was to save the URL as an attribute and use a custom JS to read from this attribute (my pages are rendered in the server, so I can add this info there).
This is not possible with AMP as you can't run custom Javascript. This means you won't be able to read from local storage either.

How can a user retrieve the current iFrame URL (cross-domain)

TL;DR : How can a user copy the current URL of an iFrame (cross-domain) ?
I want to insert in a webpage of my domain an iFrame to be able to browse other partner websites (different domains). When the user finds the page he is interested in, he needs to transfer the information (URL of the page) to my website somehow. It is fine to ask the user to do it manually, I'm just looking for the more convenient way to do that.
I am aware that cross-reference scripting restrictions to prevent XSS attacks limit what you can do with an iFrame & javascript.
I have looked at questions like this one, but the answers seem old, and from what I understand they are all trying to automatize this process with javascript, but in my case I don't need that
EDIT :
I accept a manual solution for the user. ie, a browser-specific procedure (a solution that works on Firefox/Chrome would be fine). For example, it's possible to have a look at the browser history... but I'm looking for better solutions.
Some stuff I found
Firefox
Browser history
All URLs browsed inside iFrames will still be added to the browser history
Element info
When right-clicking the iFrame, there is an additional submenu that opens in firefox (in French called "ce cadre", must be something like "this frame" in english). Then you can click on "information for this frame", and it will show many info including the URL.
I'm still looking for more convenient ways to retrieve the URL. Ideally, a shortcut would be nice !
You should be able to do something like this
jQuery("iframe").get(0).contentWindow.location.origin
Use jQuery to find the desirable iframe then use .contentWindow.location.origin to get the URL.

Dynamic web application without hashbang #!

How is it possible that web applications like Google Maps and Mixcloud update their urls without the use of a hashbang (also known as #!)?
Notice for example the coordinates right after the # sign in the URL while swiping the view in Google Maps. Or note that the music keeps playing while following some links.
I'm looking for a programmatic way to achieve the same functionality and I would also like to know how this works.
The HTML5 history API is a standardized way to manipulate the browser history via script. Part of this API — navigating the history — has been available in previous versions of HTML. The new parts in HTML5 include a way to add entries to the browser history, to visibly change the URL in the browser location bar (without triggering a page refresh), and an event that fires when those entries are removed from the stack by the user pressing the browser’s back button. This means that the URL in the browser location bar can continue to do its job as a unique identifier for the current resource, even in script-heavy applications that don’t ever perform a full page refresh.
Source: http://diveintohtml5.info/history.html
Have you taken a look at ASP.Net MVC? It uses the single page application concept. I'm not entirely sure what you're looking for but this is a good example: http://www.microsoftvirtualacademy.com/training-courses/introduction-to-asp-net-mvc
You also might want to look at AngularJs, which makes routing urls really easy.

How can I check to see if a forward history exists in Javascript? (Disabling back button)

Got a window.history.go(1) to stop the user hitting the back button, but was wondering if I could check to see if a forward history exists before doing it, so I can display a popup warning the user not to press the back button.
I know you can get the history length, but is there a way to get the current position in the history list? Or some other way of doing this...
AFAIK you won't be able to access the history from JavaScript. It will be a security hole and most of the browsers won't allow that. Probably there might be a workaround in IE by using ActiveX.
Found this entry which might be useful to you
window.history
There is a next property.
Returns the URL of the next item in
the session history
This property is not available to web
content and is not supported by other
browsers.
For security reasons the History
object doesn't allow the
non-privileged code to access the URLs
of other pages in the session history,
but it does allow it to navigate the
session history.
There is no way to clear the session
history or to disable the back/forward
navigation from unprivileged code. The
closest available solution is the
location.replace() method, which
replaces the current item of the
session history with the provided URL.
Don't break the back (or forward) button.
You don't know exactly what they might do without knowing every possible user's setup (including plugins, etc.)
You still have to do something mildly intelligent (e.g. not trash your database) if scripting is disabled, or disabled then re-enabled, etc.
Because you already have to handle #2, don't even worry about figuring out how to do #1 everywhere for everyone, and spend more time to make #2 work better.
Now your site works with the back button.

How can I tell if my page is set as the user's homepage?

Is there any way I can detect when my page has been set as the user's homepage in their browser?
I'm most interested in something in javascript, but I'd be happy to hear about other approaches as well.
Edit: I'm not looking for anything sneaky. I'm wondering if there is anything that is explicitly allowed through the browsers to find out this information.
There isn't likely to be a foolproof method, as that's an intrusion into the privacy of the user.
One thing that comes to mind is checking for a referrer. If the user arrived at your page without following a link, they a) typed the url, b) followed a bookmark, or c) have your page set as their homepage. But that's about the best I can do.
Nope. You can tell if they got to your page by following a link or not. Check the referrer. However, the browser going to your page as the home page will not appear any different than the user typing in your page or using a bookmark.
Mozilla/Firefox has a window.home() method which loads the user's home page. This method could be used (in an iframe, maybe) combined with server access logging, to see if the site's home page is instantly requested loaded by the current user.
However, other browsers don't seem to support this javascript method.
Simple solution, you shouldn't be checking if you or anyone else's site is set as the user's homepage. If they want it as their homepage, they'll make it so.
However, if you've got some Javascript that will check their email and see if they've sent links to your site to their friends or colleagues, I'd be very interested in that functionality ;-)

Categories

Resources