skip to Main Content

I added this link https://website.test/about/contact-us/?modal=example in wp backend menu editor and checked the link after I input this code that’s supposed to open the bootstrap model once the link was entered in the browser. However it didn’t work.

Here’s my javascript code that I added within <script> at bottom under </footer> tag.

<script type="text/javascript">
(function($) {

$(document).ready(function() {
 if(window.location.href.indexOf('/?modal=example') != -1) {
   $('#exampleModal').modal('show');
 }
});

})(jQuery);
</script>

Every time I try to refresh or type the url in another page, it won’t load/open the modal. What’s wrong with my code?

2

Answers


  1. Chosen as BEST ANSWER

    Actually, the url I was looking for turned out to be like this

    https://website.org/about/contact-us?modal=example
    

  2. You could try using URLSearchParams() to check for the query param.

    const params = new URLSearchParams(location.search),
          modal_param = params.get('modal'); 
    
    if(modal_param){
      // do stuff
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search