How to persist jQuery UI dialog state between requests?
I have an ASP.NET MVC web application. The application can be managed with a "remote control", which is a jQuery UI dialog and appears only when a user with sufficent permissions logs in and clicks on the "show remote control" button. (Obviously, the HTML code of the dialog is not there otherwise.)
When the user clicks on a link on the site, this dialog closes itself.
I want the dialog to "remember" that it was open and open itself automatically in such cases.What is the simplest and most cross-browser-compatible way of doing this?
(Let's assume for now that I don't want to care about archaic browsers, the people who will use this will use new browsers.)Here is the code that initializes the dialog.
$(document).ready(function () {
$("#remote-control").dialog({
autoOpen: false,
show: "explode",
hide: "explode",
title: "Távirányító",
minWidth: 220,
maxWidth: 260,
width: 190,
height: 300,
maxHeight: 500,
position: [50, 100]
});
$("#remote-c开发者_JS百科ontrol-opener").click(function () {
$("#remote-control").dialog("open");
return false;
});
});
Thanks in advance for your answers!
There are two ways you could do this. Use either a cookie or a URL parameter to set a value like isRemoteVisible
which you can check on page load. The jquery libraries I would use to achieve this are:
- Cookie approach: https://github.com/carhartl/jquery-cookie
- Url Param approach: http://www.mathias-bank.de/jQuery/jquery.getUrlParam.js
Using a url parameter is more reliable as your user may have cookies disabled. After you've detected the value of your cookie/param, you can programmtically trigger the remote control to pop up.
Edit
You can set a cookie using the library mentioned above as follows:
$.cookie('isRemoteVisible', 'true');
You can then get this cookie value as follows:
var showRemote = $.cookie('isRemoteVisible');
Finally, perform a check on this to open the dialog:
if ("true" == showRemote)
$("#remote-control").dialog("open");
精彩评论