I have a page which has a series of dynamic jquery ui dialogs which have different id's. For example:
<div id="message-1">
content
</div>
<div id="messag开发者_JS百科e-2">
content
</div>
I want some code that can launch the appropriate dialog box based on URL.
For example, if the url was http://url.com/#message-2 it would open only the appropriate dialog box.
I've been trying to use code like the following as a basis to start from, but it clearly isn't a solution.
if(window.location.href.indexOf('#message') != -1) {
$('.dialog').dialog('open');
}
Any help will be greatly appreciated.
I think you are looking for this
$(window.location.hash).dialog('open');
Use window.location.hash to get the #message-part of the url.
if(window.location.hash == '#message-1') {
$('.message-1').dialog('open');
}
This code works with #message-1, #message-2 ... #message-n
if(window.location.hash != '') {
$('.' + window.location.hash).dialog('open');
}
Note: You should probably validate input but i didn't since it's an example
精彩评论