how to get history.back value in javascript - url

How can i get the URL of the previous page in JavaScript?
I mean the value of this function:
history.back()
or this one :
history.go(-1)

For privacy reasons, you will never be able to do that.

In JavaScript, you use the document.referrer property, which will give you the URL of the page where the user came from.

Related

Constant querystring arguments in Page Tab URL

I want to point my Page Tab to a URL with querystring arguments like this:
http://domain.com/page.php?arg=CONSTANT
arg's value is a constant so I'm not trying to pass any dynamic data from the facebook page to my PHP page. It's just supposed to be a constant URL, with a constant querystring parameter.
I'm having problems getting this to work (nothing is shown on the facebook page tab), however when I use an URL without querystring parameters like this:
http://domain.com/page.php
it works right.
I couldn't find any information in the documentation saying that it's impossible to use URLs with querystring arguments. Is it? or am I doing something wrong?
Your answer lives in the something facebook calls app_data in the signed_request specifically made for page tab apps. See http://developers.facebook.com/docs/authentication/signed_request/

Rails set session hash value from link

I need to set a flag in the session hash when a link is clicked (to toggle between basic/advanced views of a page). Is there a way to do this?
I'm thinking I can do it if I add a route for something like "change_mode" and a parameter that it accepts and the method in the controller for it would just set the value in the hash and link back to the previous page, but that feels like a hack...
Thanks.
How is it a hack? That's how you get information from the browser to the server, through a request.

Is there a method in jQuery Mobile that returns the current path?

I'm looking for a method in jQuery Mobile to get the path of the current page regardless of if it has been navigated to directly or loaded through Ajax.
So for both:
http://example.com/#/page1
and:
http://example.com/page1
the same path should be returned:
/page1
I know this can probably be extracted from location.href, but it will require some additional code so I'm wondering if there is a simpler more robust way.
If you're looking for the current path or page id, you could use:
$.mobile.activePage[0].id
So I finally found that you can get this by doing:
$('.ui-page-active').data('url')
It's location.href AND location.hash
There also is a generated <base> element that contains a href you should find helpful.
Mentioned here: http://jquerymobile.com/test/#docs/pages/docs-navmodel.html
Caution: it says the documentation needs to be updated.
Also - please read a bit about progressive enhancement on which JQM bases its idea, because you might not want to deal with links from javascript.
Try this one.
$.mobile.activePage[0].baseURI
$.mobile.activePage doesn't work if you need to know page url before pageinit event. If you want to get current page url at any time you can use such a method:
getCurrentPageUri: function() {
var currentUrl = $.mobile.path.parseUrl(window.location);
var result = currentUrl.hash && currentUrl.hash.split("?")[0];
return (result && result.substring(1)) || currentUrl.pathname;
}
The solution from Reoger Ertesvag didn't work for me as it has always the same value even after pageChange. I use this one instead and it works great:
$.mobile.activePage.data("url");
Be careful as it doesn't work on pageinit but on pageshow.

Adding a "previous" link

What the appropriate way to do this?
ViewData["PreviousPage"]=Request.UrlReferrer.PathAndQuery;
this doesnt work if directly accessing.
EDIT: I did a null check on Request.UrlReferrer, seems to be fine (?)
If directly, it's impossible this way. URL referer is set only when clicking a link.
If you're interested only in "Previous Page" link working inside your website, then you can store current URL in session, and retrieve it during next request, then replace with a new current url. Ugly, but working.
Is there some reason this needs to be server-side instead of client-side? If you can deal with client side, Javascript is the answer:
<input type=button value="Back" onClick="history.go(-1)">
This uses the browser's built-in back functionality -- it essentially mimics clicking the "Back" button.
Put this somewhere in your Base Controller or Custom Filter:
TempData["PreviousPage"] = TempData["CurrentPage"];
TempData["CurrentPage"] = Request.Url;

Is there a way to change the browser's address bar without refreshing the page?

I'm developing a web app. In it I have a section called categories that every time a user clicks one of the categories an update panel loads the appropriate content.
After the user clicked the category I want to change the browser's address bar url from
www.mysite.com/products
to something like
www.mysite.com/products/{selectedCat}
without refreshing the page.
Is there some kind of JavaScript API I can use to achieve this?
With HTML5 you can modify the url without reloading:
If you want to make a new post in the browser's history (i.e. back button will work)
window.history.pushState('Object', 'Title', '/new-url');
If you just want to change the url without being able to go back
window.history.replaceState('Object', 'Title', '/another-new-url');
The object can be used for ajax navigation:
window.history.pushState({ id: 35 }, 'Viewing item #35', '/item/35');
window.onpopstate = function (e) {
var id = e.state.id;
load_item(id);
};
Read more here: http://www.w3.org/TR/html5-author/history.html
A fallback sollution: https://github.com/browserstate/history.js
To add to what the guys have already said edit the window.location.hash property to match the URL you want in your onclick function.
window.location.hash = 'category-name'; // address bar would become http://example.com/#category-name
I believe directly manipulating the address bar to a completely different url without moving to that url isn't allowed for security reasons, if you are happy with it being
www.mysite.com/products/#{selectedCat}
i.e. an anchor style link within the same page then look into the various history/"back button" scripts that are now present in most javascript libraries.
The mention of update panel leads me to guess you are using asp.net, in that case the asp.net ajax history control is a good place to start
I don't think this is possible (at least changing to a totally different address), as it would be an unintuitive misuse of the address bar, and could promote phishing attacks.
This cannot be done the way you're saying it. The method suggested by somej.net is the closest you can get. It's actually very common practice in the AJAX age. Even Gmail uses this.
"window.location.hash"
as suggested by sanchothefat should be the one and only way of doing it. Because all the places that I have seen this feature, it's all the time after the # in URL.

Resources