Andy
Andy

Reputation: 13

How do I load a dynamic jquery ui dialog from URL?

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="message-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.

Upvotes: 1

Views: 599

Answers (2)

ShankarSangoli
ShankarSangoli

Reputation: 69905

I think you are looking for this

    $(window.location.hash).dialog('open');

Upvotes: 1

Jonas Stensved
Jonas Stensved

Reputation: 15286

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

Upvotes: 0

Related Questions