开发者

firing javascript function on page load

I have a method

toggleDrawer(this)

That expands a small window component on my webpage. It has a + icon, that, when clicked, expands the window and gets replaced with a -.

There are also a form and some filter options in this window component that when submitted, reloads the page and so when it reloads, it is shown with the window component not expanded.

How d开发者_开发问答o I make it so that when I submit the form, the window is still open?

The link goes from http://link to http://link#something when I submit the form.


Something like this will do

window.onload = function() {
    if (document.location.hash === 'something') {
        someObject.toggleDrawer();
    }
};

Of course this will not work if you already have some events bound to the window.onload event. In this case you may want to use some event dispatch and add the above function to the callbacks to be invoked when the window loads. Every Javascript library has some kind of object management. For instance in jQuery you would do

$(window).load(function() {
    if (document.location.hash === 'something') {
        someObject.toggleDrawer();
    }
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜