Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass jquery object to history.pushState

var data = { url: $(this).attr('href'), selector: $(this) };
history.pushState(data, 'foo', $(this).attr('href'));

When I do this I get a error:

Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIDOMHistory.pushState]

If I change selector to a string, then the error goes away... But I need the jQuery object, so I can trigger click on it on the "popstate" event :s

like image 436
M K Avatar asked Nov 14 '11 03:11

M K


People also ask

What is jquery pushState history?

In an HTML document, the history. pushState() method adds an entry to the browser's session history stack.

Does pushState reload the page?

pushState() method # The history. pushState() method can be used to push a new entry into the browser's history—and as a result, update the displayed URL—without refreshing the page.

What is window history replaceState?

The History. replaceState() method modifies the current history entry, replacing it with the state object and URL passed in the method parameters. This method is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.


1 Answers

The data in the history state must be serializable. That is, it must be convertible to JSON. $(this) returns an array-like object with DOM nodes which cannot be converted to JSON. You should look for a different way to do what you're looking for.

Remember, the push state is just helpful information and is not meant to be canonical. If the user can't get the exact same content by copying and pasting the URL in the address bar you've got a problem.

like image 172
Brian Nickel Avatar answered Oct 04 '22 01:10

Brian Nickel