locked
How to get HTML Dialog with modal to work in a ASPX page? RRS feed

  • Question

  • User1909155429 posted

    I am trying to get this Modal example to work in a aspx page that was originally constructed for HTML page. But nothing works?

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="ModalDialog.aspx.vb" Inherits="ModalDialog" %>
    
    <!DOCTYPE html @charset "UTF-8";>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    
    
    <style>
    
    dialog {
      padding: 0;
      border: 0;
      border-radius: 0.6rem;
      box-shadow: 0 0 1em black;
    }
    
    /* native backdrop */
    dialog::-webkit-backdrop {
      background-color: rgba(0, 0, 0, 0.4);
    }
    dialog::backdrop {
      background-color: rgba(0, 0, 0, 0.4);
    }
    
    /* polyfill backdrop */
    dialog + .backdrop {
      ;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      background-color: rgba(0, 0, 0, 0.4);
    }
    
    dialog[open] {
      -webkit-animation: slide-up 0.4s ease-out;
              animation: slide-up 0.4s ease-out;
    }
    
    /* The following styles are for older browsers when using
       the polyfill. These aren’t necessary for Chrome/Firefox. */
    dialog {
      display: none;
      ;
      margin: 0 auto;
      /* should center it, but not working in Safari */
      max-width: 80vw;
      background-color: white;
    }
    
    dialog[open] {
      display: block;
    }
    
    /* prettying things up a bit */
    .close {
      ;
      top: 0.2em;
      right: 0.2em;
      padding: 0.3em;
      line-height: 0.6;
      background-color: transparent;
      border: 0;
      font-size: 2em;
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
      padding: 1em;
    }
    
    .modal-header {
      margin: 0;
      padding-bottom: 0.6em;
      background-color: #ddedfd;
      border-top-left-radius: 0.6rem;
      border-top-right-radius: 0.6rem;
      border-bottom: 1px solid #78a4d3;
    }
    
    .modal-footer {
      border-top: 1px solid #78a4d3;
    }
    
    /* misc page styles -- not important for demo */
    body {
      margin: 1em;
    }
    
    :first-child {
      margin-top: 0;
    }
    
    :last-child {
      margin-bottom: 0;
    }
    
    button {
      padding: 0.4em 1em 0.2em;
      font-family: Glober, Helvetica, sans-serif;
    }
    
    @-webkit-keyframes slide-up {
      0% {
        opacity: 0;
        transform: translate(0, 15px);
      }
      100% {
        opacity: 1;
        transform: translate(0, 0);
      }
    }
    
    @keyframes slide-up {
      0% {
        opacity: 0;
        transform: translate(0, 15px);
      }
      100% {
        opacity: 1;
        transform: translate(0, 0);
      }
    }
    </style>
        <script>
        
            const modal = document.getElementById('demo-modal');
            const open = document.getElementById('open-modal');
            const close = document.getElementById("close");
            const likeIt = document.getElementById("like-it");
            const loveIt = document.getElementById("love-it");
            const returnSpan = document.getElementById("return-value");
    
            dialogPolyfill.registerDialog(modal);
    
            open.addEventListener("click", () => {
                alert('test');
                modal.showModal(); 
            });
    
            likeIt.addEventListener("click", () => {
                modal.close("Like it");
            });
    
            loveIt.addEventListener("click", () => {
                modal.close("Love it");
            });
    
            close.addEventListener("click", () => {
                modal.close("cancelled");
            });
    
            modal.addEventListener("cancel", () => {
                modal.close("cancelled");
            });
    
            // close when clicking on backdrop
            modal.addEventListener("click", event => {
                if (event.target === modal) {
                    modal.close("cancelled");
                }
            });
    
            // display returnValue
            modal.addEventListener("close", () => {
                returnSpan.innerHTML = modal.returnValue;
            });
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
          <button id="openmodal" type="button" >Show me the modal</button>
         
    <dialog id="demo-modal" >
      <h3 class="modal-header">A native modal dialog box</h3>
      <div class="modal-body">
        <p>Finally, HTML has a native dialog box element! This is fantastic.</p>
        <p>And a polyfill makes this usable today.</p>
      </div>
      <footer class="modal-footer">
        <button id="like-it" type="button">I like it</button>
        <button id="love-it" type="button">I love it</button>
      </footer>
      <button id="close" class="close" type="button">&times;</button>
    </dialog>
    
    <p><code>returnValue</code>: <span id="return-value"></span></p>
    
    <hr>
    
    <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus porttitor porttitor interdum. Vivamus vehicula nibh sit amet sapien varius hendrerit. Suspendisse ac lobortis mauris. Praesent tincidunt, risus nec pulvinar ullamcorper, ante erat luctus ante, eu suscipit elit purus sit amet turpis. In hac habitasse platea dictumst. Nulla lobortis libero non pulvinar tempus. Nulla tempor luctus sapien faucibus efficitur. Curabitur maximus nibh id sapien facilisis, vitae ultricies sem convallis. Nulla fermentum eget diam et finibus. Sed efficitur justo sed mollis ultrices. Sed pharetra dapibus nisi, non venenatis purus bibendum at. Integer risus mauris, faucibus non efficitur nec, viverra in mauris.</p>
    
    <p>In lobortis arcu ac lobortis pulvinar. Aenean libero tellus, cursus id tortor non, iaculis dapibus diam. Nullam id nisi odio. Vivamus pellentesque lacinia turpis, eu eleifend justo posuere vel. Nulla pharetra nec justo eu facilisis. Vivamus sollicitudin, est ut varius eleifend, justo metus ultrices dui, ut scelerisque nisi odio vel est. Pellentesque nec tortor tristique, tempor velit at, lobortis augue. Nunc nunc neque, accumsan a placerat accumsan, rutrum sit amet turpis.</p>
    
    <hr>
    
    <p>See my <a href="https://keithjgrant.com/posts/2018/meet-the-new-dialog-element/">&lt;dialog&gt; element write-up</a> for more details.</p>
        </form>
    </body>
    </html>
    

    Friday, February 12, 2021 3:17 PM

Answers

  • User-943250815 posted

    Your script is running before DOM Tree gets filled.
    Just move <script> block to the end of page, right after </form> or </body>
    Change const open = document.getElementById('open-modal'); to const open = document.getElementById('openmodal');
    Remove or comment dialogPolyfill.registerDialog(modal);
    Test it in Chrome or Edge Chromium

    Here another way to do same https://css-tricks.com/some-hands-on-with-the-html-dialog-element/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2021 2:16 PM
  • User1535942433 posted

    Hi peterthegreat,

    According to your description,I have created a test and I find three problems.

    1. JS need to be write after body content.
    2. You need add dialog-polyfill js.
    3. Spelling wrong.

    You could follow these codes:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="2174412.aspx.vb" Inherits="vb._2174412" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link href="Content/dialog-polyfill.css" rel="stylesheet" />
        <style>
            dialog {
                padding: 0;
                border: 0;
                border-radius: 0.6rem;
                box-shadow: 0 0 1em black;
            }
    
                /* native backdrop */
                dialog::-webkit-backdrop {
                    background-color: rgba(0, 0, 0, 0.4);
                }
    
                dialog::backdrop {
                    background-color: rgba(0, 0, 0, 0.4);
                }
    
                /* polyfill backdrop */
                dialog + .backdrop {
                    ;
                    top: 0;
                    right: 0;
                    bottom: 0;
                    left: 0;
                    background-color: rgba(0, 0, 0, 0.4);
                }
    
                dialog[open] {
                    -webkit-animation: slide-up 0.4s ease-out;
                    animation: slide-up 0.4s ease-out;
                }
    
            /* The following styles are for older browsers when using
       the polyfill. These aren’t necessary for Chrome/Firefox. */
            dialog {
                display: none;
                ;
                margin: 0 auto;
                /* should center it, but not working in Safari */
                max-width: 80vw;
                background-color: white;
            }
    
                dialog[open] {
                    display: block;
                }
    
            /* prettying things up a bit */
            .close {
                ;
                top: 0.2em;
                right: 0.2em;
                padding: 0.3em;
                line-height: 0.6;
                background-color: transparent;
                border: 0;
                font-size: 2em;
            }
    
            .modal-header,
            .modal-body,
            .modal-footer {
                padding: 1em;
            }
    
            .modal-header {
                margin: 0;
                padding-bottom: 0.6em;
                background-color: #ddedfd;
                border-top-left-radius: 0.6rem;
                border-top-right-radius: 0.6rem;
                border-bottom: 1px solid #78a4d3;
            }
    
            .modal-footer {
                border-top: 1px solid #78a4d3;
            }
    
            /* misc page styles -- not important for demo */
            body {
                margin: 1em;
            }
    
            :first-child {
                margin-top: 0;
            }
    
            :last-child {
                margin-bottom: 0;
            }
    
            button {
                padding: 0.4em 1em 0.2em;
                font-family: Glober, Helvetica, sans-serif;
            }
    
            @-webkit-keyframes slide-up {
                0% {
                    opacity: 0;
                    transform: translate(0, 15px);
                }
    
                100% {
                    opacity: 1;
                    transform: translate(0, 0);
                }
            }
    
            @keyframes slide-up {
                0% {
                    opacity: 0;
                    transform: translate(0, 15px);
                }
    
                100% {
                    opacity: 1;
                    transform: translate(0, 0);
                }
            }
        </style>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <button id="openmodal" type="button">Show me the modal</button>
    
            <dialog id="demo-modal">
                <h3 class="modal-header">A native modal dialog box</h3>
                <div class="modal-body">
                    <p>Finally, HTML has a native dialog box element! This is fantastic.</p>
                    <p>And a polyfill makes this usable today.</p>
                </div>
                <footer class="modal-footer">
                    <button id="like-it" type="button">I like it</button>
                    <button id="love-it" type="button">I love it</button>
                </footer>
                <button id="close" class="close" type="button">&times;</button>
            </dialog>
    
            <p><code>returnValue</code>: <span id="return-value"></span></p>
    
            <hr>
    
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus porttitor porttitor interdum. Vivamus vehicula nibh sit amet sapien varius hendrerit. Suspendisse ac lobortis mauris. Praesent tincidunt, risus nec pulvinar ullamcorper, ante erat luctus ante, eu suscipit elit purus sit amet turpis. In hac habitasse platea dictumst. Nulla lobortis libero non pulvinar tempus. Nulla tempor luctus sapien faucibus efficitur. Curabitur maximus nibh id sapien facilisis, vitae ultricies sem convallis. Nulla fermentum eget diam et finibus. Sed efficitur justo sed mollis ultrices. Sed pharetra dapibus nisi, non venenatis purus bibendum at. Integer risus mauris, faucibus non efficitur nec, viverra in mauris.</p>
    
            <p>In lobortis arcu ac lobortis pulvinar. Aenean libero tellus, cursus id tortor non, iaculis dapibus diam. Nullam id nisi odio. Vivamus pellentesque lacinia turpis, eu eleifend justo posuere vel. Nulla pharetra nec justo eu facilisis. Vivamus sollicitudin, est ut varius eleifend, justo metus ultrices dui, ut scelerisque nisi odio vel est. Pellentesque nec tortor tristique, tempor velit at, lobortis augue. Nunc nunc neque, accumsan a placerat accumsan, rutrum sit amet turpis.</p>
    
            <hr>
    
            <p>See my <a href="https://keithjgrant.com/posts/2018/meet-the-new-dialog-element/">&lt;dialog&gt; element write-up</a> for more details.</p>
            <script src="Scripts/jquery-1.10.2.min.js"></script>
            <script src="Scripts/dialog-polyfill.js"></script>
            <script>
    
                const modal = document.getElementById('demo-modal');
                const open = document.getElementById('openmodal');
                const close = document.getElementById("close");
                const likeIt = document.getElementById("like-it");
                const loveIt = document.getElementById("love-it");
                const returnSpan = document.getElementById("return-value");
    
                dialogPolyfill.registerDialog(modal);
    
                open.addEventListener("click", () => {
                    alert('test');
                    modal.showModal();
                });
    
                likeIt.addEventListener("click", () => {
                    modal.close("Like it");
                });
    
                loveIt.addEventListener("click", () => {
                    modal.close("Love it");
                });
    
                close.addEventListener("click", () => {
                    modal.close("cancelled");
                });
    
                modal.addEventListener("cancel", () => {
                    modal.close("cancelled");
                });
    
                // close when clicking on backdrop
                modal.addEventListener("click", event => {
                    if (event.target === modal) {
                        modal.close("cancelled");
                    }
                });
    
                // display returnValue
                modal.addEventListener("close", () => {
                    returnSpan.innerHTML = modal.returnValue;
                });
            </script>
        </form>
    </body>
    </html>

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2021 9:13 AM

All replies

  • User-943250815 posted

    Give a try to SweetAlert2

    Friday, February 12, 2021 8:04 PM
  • User1909155429 posted

    Thats a nice application thanks.

    Though i would still like to know why the code provided does not work in aspx file?

    I know that i am unable to reference the click handlers for some reason. I would expect to be able considering i am operating windows 10 and up to date browser. are they not compatible in a aspx page ?

    Sunday, February 14, 2021 11:08 PM
  • User-943250815 posted

    Your script is running before DOM Tree gets filled.
    Just move <script> block to the end of page, right after </form> or </body>
    Change const open = document.getElementById('open-modal'); to const open = document.getElementById('openmodal');
    Remove or comment dialogPolyfill.registerDialog(modal);
    Test it in Chrome or Edge Chromium

    Here another way to do same https://css-tricks.com/some-hands-on-with-the-html-dialog-element/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2021 2:16 PM
  • User1535942433 posted

    Hi peterthegreat,

    According to your description,I have created a test and I find three problems.

    1. JS need to be write after body content.
    2. You need add dialog-polyfill js.
    3. Spelling wrong.

    You could follow these codes:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="2174412.aspx.vb" Inherits="vb._2174412" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link href="Content/dialog-polyfill.css" rel="stylesheet" />
        <style>
            dialog {
                padding: 0;
                border: 0;
                border-radius: 0.6rem;
                box-shadow: 0 0 1em black;
            }
    
                /* native backdrop */
                dialog::-webkit-backdrop {
                    background-color: rgba(0, 0, 0, 0.4);
                }
    
                dialog::backdrop {
                    background-color: rgba(0, 0, 0, 0.4);
                }
    
                /* polyfill backdrop */
                dialog + .backdrop {
                    ;
                    top: 0;
                    right: 0;
                    bottom: 0;
                    left: 0;
                    background-color: rgba(0, 0, 0, 0.4);
                }
    
                dialog[open] {
                    -webkit-animation: slide-up 0.4s ease-out;
                    animation: slide-up 0.4s ease-out;
                }
    
            /* The following styles are for older browsers when using
       the polyfill. These aren’t necessary for Chrome/Firefox. */
            dialog {
                display: none;
                ;
                margin: 0 auto;
                /* should center it, but not working in Safari */
                max-width: 80vw;
                background-color: white;
            }
    
                dialog[open] {
                    display: block;
                }
    
            /* prettying things up a bit */
            .close {
                ;
                top: 0.2em;
                right: 0.2em;
                padding: 0.3em;
                line-height: 0.6;
                background-color: transparent;
                border: 0;
                font-size: 2em;
            }
    
            .modal-header,
            .modal-body,
            .modal-footer {
                padding: 1em;
            }
    
            .modal-header {
                margin: 0;
                padding-bottom: 0.6em;
                background-color: #ddedfd;
                border-top-left-radius: 0.6rem;
                border-top-right-radius: 0.6rem;
                border-bottom: 1px solid #78a4d3;
            }
    
            .modal-footer {
                border-top: 1px solid #78a4d3;
            }
    
            /* misc page styles -- not important for demo */
            body {
                margin: 1em;
            }
    
            :first-child {
                margin-top: 0;
            }
    
            :last-child {
                margin-bottom: 0;
            }
    
            button {
                padding: 0.4em 1em 0.2em;
                font-family: Glober, Helvetica, sans-serif;
            }
    
            @-webkit-keyframes slide-up {
                0% {
                    opacity: 0;
                    transform: translate(0, 15px);
                }
    
                100% {
                    opacity: 1;
                    transform: translate(0, 0);
                }
            }
    
            @keyframes slide-up {
                0% {
                    opacity: 0;
                    transform: translate(0, 15px);
                }
    
                100% {
                    opacity: 1;
                    transform: translate(0, 0);
                }
            }
        </style>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <button id="openmodal" type="button">Show me the modal</button>
    
            <dialog id="demo-modal">
                <h3 class="modal-header">A native modal dialog box</h3>
                <div class="modal-body">
                    <p>Finally, HTML has a native dialog box element! This is fantastic.</p>
                    <p>And a polyfill makes this usable today.</p>
                </div>
                <footer class="modal-footer">
                    <button id="like-it" type="button">I like it</button>
                    <button id="love-it" type="button">I love it</button>
                </footer>
                <button id="close" class="close" type="button">&times;</button>
            </dialog>
    
            <p><code>returnValue</code>: <span id="return-value"></span></p>
    
            <hr>
    
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus porttitor porttitor interdum. Vivamus vehicula nibh sit amet sapien varius hendrerit. Suspendisse ac lobortis mauris. Praesent tincidunt, risus nec pulvinar ullamcorper, ante erat luctus ante, eu suscipit elit purus sit amet turpis. In hac habitasse platea dictumst. Nulla lobortis libero non pulvinar tempus. Nulla tempor luctus sapien faucibus efficitur. Curabitur maximus nibh id sapien facilisis, vitae ultricies sem convallis. Nulla fermentum eget diam et finibus. Sed efficitur justo sed mollis ultrices. Sed pharetra dapibus nisi, non venenatis purus bibendum at. Integer risus mauris, faucibus non efficitur nec, viverra in mauris.</p>
    
            <p>In lobortis arcu ac lobortis pulvinar. Aenean libero tellus, cursus id tortor non, iaculis dapibus diam. Nullam id nisi odio. Vivamus pellentesque lacinia turpis, eu eleifend justo posuere vel. Nulla pharetra nec justo eu facilisis. Vivamus sollicitudin, est ut varius eleifend, justo metus ultrices dui, ut scelerisque nisi odio vel est. Pellentesque nec tortor tristique, tempor velit at, lobortis augue. Nunc nunc neque, accumsan a placerat accumsan, rutrum sit amet turpis.</p>
    
            <hr>
    
            <p>See my <a href="https://keithjgrant.com/posts/2018/meet-the-new-dialog-element/">&lt;dialog&gt; element write-up</a> for more details.</p>
            <script src="Scripts/jquery-1.10.2.min.js"></script>
            <script src="Scripts/dialog-polyfill.js"></script>
            <script>
    
                const modal = document.getElementById('demo-modal');
                const open = document.getElementById('openmodal');
                const close = document.getElementById("close");
                const likeIt = document.getElementById("like-it");
                const loveIt = document.getElementById("love-it");
                const returnSpan = document.getElementById("return-value");
    
                dialogPolyfill.registerDialog(modal);
    
                open.addEventListener("click", () => {
                    alert('test');
                    modal.showModal();
                });
    
                likeIt.addEventListener("click", () => {
                    modal.close("Like it");
                });
    
                loveIt.addEventListener("click", () => {
                    modal.close("Love it");
                });
    
                close.addEventListener("click", () => {
                    modal.close("cancelled");
                });
    
                modal.addEventListener("cancel", () => {
                    modal.close("cancelled");
                });
    
                // close when clicking on backdrop
                modal.addEventListener("click", event => {
                    if (event.target === modal) {
                        modal.close("cancelled");
                    }
                });
    
                // display returnValue
                modal.addEventListener("close", () => {
                    returnSpan.innerHTML = modal.returnValue;
                });
            </script>
        </form>
    </body>
    </html>

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2021 9:13 AM