简体   繁体   中英

Open uikit modal on page load

I'm currently working on a project in Flask and I'd like for (under specific circumstances) open up a modal when the page loads. Currently what I have is as follows:

My view function passes a variable 'open_modal' into the template

    return render_template('index.html', open_modal = "window.onload() = function(){UIkit.modal(#modal1).show();};")

and inside the template itself, there's a part in the template which looks like:

<script type="text/javascript">
    {{ open_modal }}
</script>

And the modal itself looks something like:

    <div id="modal1" uk-modal>
        <div class="uk-modal-dialog uk-modal-body">
            Test test!
        </div>
    </div>

And so what I was hoping is that whenever I use the render_template with the kwarg 'open_modal' as that value above, it'd insert the javascript fragment into the template and then ta-dah! The modal would open when the page loads. However, this doesn't seem to work, and I can't figure out why. Admittedly, I have no experience with javascript so I would have no clue if the script fragment that is inserted by 'open_modal' even works, but I'd rather stick to vanilla javascript because this is the only bit of javascript in my program, and I'd rather not add more bulk to it unnecessarily with jquery etc.

Thanks a bunch if you can help!

The onload is not a function, it's a property of window . You have to assign a function to it:

return render_template('index.html', open_modal = "window.onload = function(){UIkit.modal('#modal1').show();};")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM