user1704524
user1704524

Reputation: 458

Load bootstrap Modal on first visit to site

I am trying to implement a subscription box that activates the first time you visit the website.

At the moment the subscription modal doesnt load when you first visit the site and I can't figure out why?

<script type="text/javascript">
jQuery.noConflict();
jQuery(document).load(function(){
         // load the overlay
        if (document.cookie.indexOf('visited=true') == -1) {
            var fifteenDays = 1000*60*60*24*15;
            var expires = new Date((new Date()).valueOf() + fifteenDays);
            document.cookie = "visited=true;expires=" + expires.toUTCString();
            jQuery.modal({width:"580px", inline:true, href:"#myModal"});
        }
    });
</script>

I can load the modal by click a link but I need to automatically popup when a user first visits. Can anyone see what is wrong with my code?

Upvotes: 1

Views: 5165

Answers (3)

vipin
vipin

Reputation: 1

$(document).ready(function(){

    if (document.cookie.indexOf('visited=true') == -1) {
        var fifteenDays = 1000*60*60*24*15;
        var expires = new Date((new Date()).valueOf() + fifteenDays);
        document.cookie = "visited=true;expires=" + expires.toUTCString();
        $('#flag').trigger('click');
     $('#myModal').modal('toggle');
    }

});

This is my script for you.

Upvotes: 0

patriks
patriks

Reputation: 250

There is no href option to the modal function in bootstrap. What you want to do is target the actual modal element and then call the modal function on it to activate it as a modal. Like so;

 jQuery('#myModal').modal(options)

Check out the documentation for available options

Also, example jsfiddle here

Upvotes: 0

Ren&#233; Wolferink
Ren&#233; Wolferink

Reputation: 3548

First you're referring to:

jQuery('#myModal')

And later you are referring to:

jQuery('.myModal')

Is it a class or an id? Probably that is your mistake.

On a side-note, you may wish to specify the language-attribute on your script tag.

Upvotes: 2

Related Questions