开发者

Passing jQuery object through History.pushState() function

开发者 https://www.devze.com 2023-03-03 20:49 出处:网络
I\'m using History.pushState() function, I\'m saving into push state object an jQuery object, but when statechange event is fired I\'m retriving state object data without jQuery object, but only \"nor

I'm using History.pushState() function, I'm saving into push state object an jQuery object, but when statechange event is fired I'm retriving state object data without jQuery object, but only "normal" object:


var object = {link : $("#varianty")};

console.log(object.link);   

History.pushState(object,"test","test.php");

Console returns jQuery object

jQuery(div#varianty.box)

But when the statechange event is fired:

History.Adapter.bind(window,'statechange',function(){       

        var State = History.getState();

        var link =开发者_运维知识库 State.data.link;

        console.log(link);     
});

Console return an object

Object { length=1, 0={...}, more...}

Detailed dump

0 =>    Object { jQuery1304643255828=39, constructor={...}}

context =>  Object { location={...}}

length =>   1

selector  =>    "#varianty"

Is there any chance to retrive in state object jQuery object, if no, there is not cool way but I can again select the object by: $(State.data.link.selector);

But then is useless to pass jQuery object through pushState object...

Need it to push $(this) object on click event.

Thanks for any advice or solution! :)


Objects that you pass to the pushState function seem to loose their prototype when yielded in the popstate event. To get around this you can re-attach the prototype when listening to the event.

In your case though it might be better to pass just the selector for the element you need into pushState

history.pushState({selector: "#my-div"}, "test", "/test")

That way you can just grab the element from the dom on the popstate event with jQuery

history.onpopstate = function (event) {
  if (event.state) {
    var myElem = $(event.state.selector)
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消