0

I'm using jQuery UI Dialog for pop-ups that read from external pages. How do I get it to load the title from the external HTML instead of having the change it in the code every time? Because I want different titles as well.

Any suggestions please?

function openDialog(url) {
   $("<div class='popupDialog'>Loading...</div>")
        .dialog({
            autoOpen: true,
            closeOnEscape: true,
            width: '900',
            height: '900',
            modal: true,
            title: 'Bonus Features',
            beforeClose: function(){   $(this).remove();   }
        }).bind('dialogclose', function() {
            jdialog.dialog('destroy');
        }).load(url, function() {
            $(this).dialog("option", "position", ['center', 'center'] );
        });

        adjustJQueryDialogOverlay();
    }

    $(window).resize(function() {
        $(".ui-dialog-content").dialog("option", "position", ['center', 'center']);
    });
3
  • You'll have to play with it a little to get it to use the title of the page you're loading and not the page you're on. Are you loading actual pages or just some text or xml? Commented May 12, 2011 at 23:22
  • I just need it to read from whichever page it loads. Actual pages. I wouldn't know how to play with it through, unfortunately. Commented May 13, 2011 at 1:16
  • You're able to load the page into the dialog. . . then check the length of $('title').length and see if it is two (one for the "host" page and one for the "dialog" page). If you get that far, try to determine which title is which: get the title of the "host" page before you load the additional page into the dailog and compare it against the text of the titles returned in your jQuery array. Commented May 13, 2011 at 20:16

2 Answers 2

1

What you seem to have are two questions. First, the simple one. How do you change the title?

The jQuery Dialog title comes from the title parameter. In your code, you have:

title: 'Bonus Features'

But if you were to change your function signature to accept a title parameter, you could pass the title along to the function and not have to worry about changing your code all the time.

function openDialog(url, title) {
   $("<div class='popupDialog'>Loading...</div>").dialog({
   ...
   title: title,
   ...
   );
}

Now for the complex one: How do you get the title from your target page. I'm not sure there's an easy way for you to pull this off short of requesting the page through AJAX and parsing the result as XML/Text. Here's a link to do that if you're really interested.

http://forum.jquery.com/topic/getting-title-tag-from-html-page-using-ajax

But I really don't want to encourage that. I mean, it seems a little overboard to ask the client script to do all that. And you're not guaranteed that it may work from browser to browser. Rather it would be much easier just to associate whatever you're using to launch your dialogs with the page titles by hard-coding the titles.

Sign up to request clarification or add additional context in comments.

2 Comments

That sounds even more complicated, which is what I need to get away from. I want the title of the HTML to be the one for the dialog itself. I guess that's impossible. I really appreciate your time and effort in your answer. Thank you so much.
No problem. And you're right, it is complicated but not impossible. The net effect would be that you'd have a delay while the browser loaded the page to parse the title before it displayed the dialog.
0

Put this in 'open' event:

$(this).parent().find('.ui-dialog-title').html('Printed Card Designer');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.