How do I start opening an href link in a jQuery dialog?

So, I have the following code shown below to create a dialog using a div on a specific page:

    $('#modal').dialog({
    autoOpen: false,
    width: 600,
    height: 450,
    modal: true,
    resizable: false,
    draggable: false,
    title: 'Enter Data',
    close: function() { 
        $("#modal .entry_date").datepicker('hide');
    } 
 });

 $('.modal').click(function() {
    $('#modal').dialog('open');
 });

Everything is working fine. However, now what I want to do is also open the link in the dialog box. For example, using something according to the code below:

<a href="/path/to/file.html" class="modal">Open Me!!</a>

I did this before, hard-coding the path, as in the code example below:

$('#modal').load('/path/to/file.html').dialog('open');

In this case, however, we cannot hard-code the path in javascript, as there will be several elements from the database.

At this moment, I'm struggling to figure out how to make this work. I am also convinced that the answer is really obvious, and I’m just determined to put up with smart people here at StackOverflow.

, , , , - , .

+3
3

href

$('.modal').click(function(e) {
    e.preventDefault();
    $('#modal').load(this.href).dialog('open');
});
+6

           

<script type="text/javascript">

    function tb_parseQuery(query) {
        var Params = {};
        if (!query) { return Params; }// return empty object
        var Pairs = query.split(/[;&]/);
        for (var i = 0; i < Pairs.length; i++) {
            var KeyVal = Pairs[i].split('=');
            if (!KeyVal || KeyVal.length != 2) { continue; }
            var key = unescape(KeyVal[0]);
            var val = unescape(KeyVal[1]);
            val = val.replace(/\+/g, ' ');
            Params[key] = val;
        }
        return Params;
    }
    $(document).ready(function () {
        $('a.uimodal').bind('click', function () {

        var $this = $(this);
        var url = $this.attr("href");



        var queryString = url.replace(/^[^\?]+\??/, '');
        var params = tb_parseQuery(queryString);
        TB_WIDTH = (params['width'] * 1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
        TB_HEIGHT = (params['height'] * 1) + 40 || $(document).height(); //defaults to 440 if no paramaters were added to URL
            TB_Title = (params['title']);



        $('<div>').dialog({
            modal: true,
            open: function () {
                $(this).load(url);
            },
            height: TB_HEIGHT,
            width: TB_WIDTH,
            title: TB_Title
        });
        return false;
    });
    });
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <a class="uimodal" href="Dialog.aspx?height=350&width=700&title=تست"> click</a>
    </div>
    </form>
</body>
</html>
+1

Phil, you need to take the attribute href:

var link = $('#modal').attr('href');
$('#modal').load(href).dialog('open');
0
source

Source: https://habr.com/ru/post/1738074/


All Articles