Website with hashchange - querying if popstate event is available? - javascript

I've never worked with the HTML5 onpopstate/pushState Method. However I'm building (or have already built) a website where scrolling through the page changes the hash in the addressbar to the ID of the currently shown element in the viewport. So I have a kind of browserhistory and Deep Linking on my single-page-layout.
I'm still doing this with setting the top.location.hash and using the hashchange() function. Now I've come across this new HTML5 popstate/onPushstate Method and thought this would be intersting …
First of: What are the benefits of using this popstate/onPushState Methods? Is it better using this than the classical way of using top.location.hash? Is the performance better?
If so, is there a way to query or find out if the current browser is supporting this Methods? So I can use those methods for modern browsers and fallback on the old way in older browsers.
Is that possible? If yes, than how?

What are the benefits of using this popstate/onPushState Methods?
You get real URLs that don't depend on JavaScript to function.
If so, is there a way to query or find out if the current browser is supporting this Methods?
if (history && history.pushState)
… or just use a compatibility library

You can do more things with the html5 history api.
You can replace the current item in the history or create a new one. With the hashtag solution you can only add new ones.
When you create or replace a history entry you can also change the url and pass an javascript object that will be returned to you in popstate which can contain any data.
A very good example for a good use of this api is github (http://www.github.com). At start they sent the full page once, after that they change the page and url to match the new page that you would load on initial request. (this means that refresh works very nicely)
if you want to test for history support you can do this like this:
if (history && history.pushState)

Related

How to restore native browser object without creating iframe?

I make an embedded JS code which works on thousands of different sites. I definitely want to use some standard browser features (like window.JSON.stringify), but sometimes it simply doesn't work, because a site has some code that ran before mine and replaced native object with something incompatible with my expectations (and standards of course).
Unfortunately the way of restoring native functions by deleting them (delete JSON.stringify) doesn't work in this case because I need to restore a whole object, not only its method. I've heard about a handy way to restore native object, which is iframe creation for getting a new window instance:
function getNativeJSON() {
if (!window.JSON || !/native code/.test(window.JSON.stringify.toString()) {
var iframe = document.createElement('iframe');
document.documentElement.appendChild(iframe);
return iframe.contentWindow.JSON;
}
return window.JSON;
}
This approach works fine for me, but sometimes (very rarely, about 1 of 10000 executions) fails. I guess this can be a consequence of some weird behavior like iframe that isn't loaded yet or anything else. Thus this method is neither elegant enough (or I just don't like it), nor can be easily reproduced and debugged.
Maybe do you know any way to restore native browser objects, like JSON?
UPDATE: Of course the iframe I use is appended to DOM before.

jQuery.data support across windows

I'm trying to update a web app that uses the jQuery.data() function to store information. The update involves refactoring the interface so that there are separate windows for different types of information rather than using just divs on the same page. Because of the way some plugins work the code that calls them has been moved into different windows to run in the window where they are needed. However, many of the callbacks used by these plugins use .data() to find stored information, but then code that sets the data stored by .data() is in the parent window, and it does not seem to attach the data to the DOM, it stores it somewhere attached to the window, so in the child window the callbacks can no longer find the data they are looking for.
Will it work using call(parent, DOMelement.data);? And is there possibly a tidier way of dealing with this?
Thanks in advance!
Thanks for all the suggestions. This wasn't using cookies because the information doesn't need to be stored beyond the current session. All of the interaction is done via javascript, there are no server requests until you save at the end. The windows that have to communicate are all open at the same time, hence the call() suggestion. I ended up solving this using .prop in place of .data as the syntax and functionality was almost identical. This directly associates the data with the DOM on the relevant window. $_SESSION only works when each page is requested from the server.
So, if you are trying to store information that needs to be accessed by multiple windows simultaneously or without a page refresh you can use the jQuery .prop() function to attach data to the DOM. .attr() could also work, but .prop() allows you to directly access values using . notation.
The .data functions are designed to prevent circular references but they store the information somewhere that means you can't access it without using .data which stores info separately for each context.

Which JavaScript hashchange/history library should I use today (2011)?

1) Which JavaScript hashchange/history library/method should I use for my JavaScript application?
2) And how do I achieve these 3 things using that method?
A) When the page loads I want to parse the url hash/parameters and set the initial application state.
Specifically, my application consists of a Backbone Collection with two models which has attributes such as selectedCountry, selectedYear etc.
(I don't think I can use the Backbone.Router as this is a very customized visualization app with complex states?)
B) I want to set up a hashchange listener or similar that lets me update the app state correspondigly
C) On Backbone Collection change events I'd like to update the url. Important: I want to remove the hashchange listener temporarily while doing this so that there is no feedback loop.
Note:
The app is already relying on some HTML5 technologies so the solution does not have to be compatible with the older browsers ... But the "feedback loop" part is important as I've struggled with this before ...
Thanks :)
since your already using Backbone, I would stick with Backbone's Router objects. It will be easier to use what's already available in your app instead of trying to bring something new in the mix.
As for your feedback loop problem, I've solved this by never firing my router methods from code.
The gist of it is that I let my JavaScript objects control the state of the application, and do the work for me. When I do call router.navigate, I never pass true as the second argument. I only call router.navigate in response to a state change in my app, to update the hash fragment in my browser window. Here's the thing: This is purely a response to the state of the application having changed. I never use router.navigate to change the state of my app.
Hope that helps

How to pass javascript object from one page to other

I want to pass javascript object from one page to other page so anyone can tell me how to do it?
Is that possible to do so using jQuery?
Few ways
Server side postback
Have a POST form on your page and save your serialized object inside a hidden input then post it to the other page. You will be able to process that data on the server and most likely put it back somehow into the page. Either as javascript object or anything else.
Client side URL examination
Make a GET request to your other page by attaching your serialized object to URL as:
http://www.app.com/otherpage.xyz?MyObject=SerializedData
That other page can then easily parse its URL and deserialize data using Javascript.
What's in a window.name = local cross-page session
This is a special technique that's also used in a special javascript library that exposes window.name as a dictionary, so you can save many different objects into it and use it as local cross-page-session. It has some size limitations that may affect you, but check the linked page for that and test your browsers.
HTML5 local storage
HTML5 has the ability of local storage that you can use exactly for these purposes. But using it heavily depends on your browser requirements. Modern browsers support it though and data can be restored even after restarting browsers or computers...
Cookies
You can always use cookies but you may run into their limitations. These days cookies are not the best choice even though they have the ability to preserve data even longer than current window session.
Javascript object serialization
You will of course have to use some sort of a (de)serializer on your client side in some of the upper cases. Deserializers are rather easy to find (jQuery already includes a great function $.getJSON()) and are most likely part of your current javascript library already (not to even mention eval()).
But for object to JSON string serialization I'd recommend json2.js library that's also recommended by John Resig. This library uses in-browser implemented JSON (de)serialization features if they exist or uses it's own implementation when they don't. Hence recommendation.
That is only possible if the pages exist at the same time, and one page is opened from the other so that you have a reference to the other pages windows object.
If you navigate from one page to another, they don't exist at the same time, so you can't communicate like that. You would have to serialise the object into a string that you can send along in the request, for example sending JSON in the query string.
There are different ways of persisting data, like in the query string, post data, cookies, window name or HTML5 local storage, but all those methods can only persist string values, not Javascript objects.
This is possible to do, and you have a couple of options.
Local Storage
Can be added/eddited/removed at any stage and accessed across a domain. (doesn't work natively in ie6 and ie7 however there are work arounds for that)
The Window Object
I would put a massive cavet around this not being the best solution, it's not at all secure, so only use it for things that don't need to be kept private. window.name = { "json" : "object"} which is then available in the following page in the window.name property.
I believe that the only way to pass a javascript object from one page to another is to serialize it into string and pass it in url. For example if you have object
var temp = { id: 1, value: 'test' }
you may want to use JSON-js to serialize it and pass it in for example http://mysite.com/?data=serialization. Then after you load the page you need to deserialize it via for example $.parseJSON().
If your application uses sessions, you could serialize it (as per other answers) then POST it to the server where it is stored in a session variable. In the next page, you retrieve it from the session variable.
In Javascript, normally all variables only exist in a scope that is unique to that page load. They don't persist between different pages if there is a new page load.
The exceptions to this are
Cookies.
Local storage.
Cookies are truly cross-browser but are extremely limited in terms of size. You shouldn't expect to be able to store more than 4kB of cookies for a page reliably; in fact you probably shouldn't be using any more than 1kB. Cookie data slows down the loading of every page and other request, so it should be used sparingly.
There are various types of local storage available to Javascript, but the only practical cross-browser implementation of this is HTML5 webstorage which is implemented in all modern browsers (IE8+, FF, Chrome, Safari, etc), but is notably not implemented in IE6 or IE7, if that matters.
Both these approaches store a value in the user's browser which can be made to be persistent so that it can be written to and read from by pages from the same site, even between page views (and even, often, between browser sessions or computer reboots).
I wrote a library some time ago, that can store most js objects to localstorage. For example instances of your prototype classes, with references to other objects, self references included. Bare in mind that IE support is lackluster.

Access URLs in the Window's History Object

Is there any way to randomly access the URLs in Javascript's History object in Safari? I'm writing an extension where I need to, on a specifically-formatted page request, capture the URL of the previous page. From what I've been able to find, the History object definition is non-standard across browsers. Safari only seems to expose its length property and the standard methods that actually move within the history. Where other implementations expose current, previous and next properties, I can't see anything that tells me Safari does the same.
I've also tried document.referrer, but that doesn't appear to get populated in this case.
I just need to display the previously accessed URL on a given page. Is there any other way to access that URL?
Thanks.
You can't really do this, at least in any white-hat way. By design. You can step the user backward and forward, but you can't see the URLs.
Less scrupulous script-writers have of course taken this as a challenge. I believe the closest they've come is to dynamically write a bunch of known comparison links to the page, and then inspect them to see if they're showing in the "visited" color state. Perhaps if you're working in a closed and predictable environment (an intranet app?), with a known set of URLs, this might be a valid approach for you. Then again, in such an environment you could deal with this on the server side with session management.

Categories

Resources