最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Can I trigger an event with javascript redirect? - Stack Overflow

programmeradmin3浏览0评论

I have a redirect:

<script>setTimeout(function() { window.location = '/'; }, 2000);</script>

That works perfectly, but I'd like to "trigger the event":

$('#languagepopup').modal('show')

at the same time.

In html I would use a button with something like:

onclick="$('#languagepopup').modal('show')"

How can I do that with the redirect?

Any help highly appreciated!

I have a redirect:

<script>setTimeout(function() { window.location = '/'; }, 2000);</script>

That works perfectly, but I'd like to "trigger the event":

$('#languagepopup').modal('show')

at the same time.

In html I would use a button with something like:

onclick="$('#languagepopup').modal('show')"

How can I do that with the redirect?

Any help highly appreciated!

Share Improve this question asked Jan 18, 2015 at 22:40 tomtomtomtom 991 gold badge1 silver badge10 bronze badges 9
  • 2 You need to pass a parameter to the page and read it with JS there. – SLaks Commented Jan 18, 2015 at 22:42
  • possible duplicate of Event when window.location.href changes – Etheryte Commented Jan 18, 2015 at 22:44
  • Do you mean show the modal when the page you are redirecting to loads? – unobf Commented Jan 18, 2015 at 22:52
  • You cannot do this at the same time since the current context will be lost as soon as the page is requested. However you can handle this on the page you are redirecting to. – IsakBosman Commented Jan 18, 2015 at 22:59
  • @unobf yes, that is my objective – tomtom Commented Jan 18, 2015 at 22:59
 |  Show 4 more ments

1 Answer 1

Reset to default 3

Set a hash in the URL like this:

<script>setTimeout(function() { window.location = '/#showModal'; }, 2000);</script>

and then in the ready handler, look for the hash and show your modal

$(document).ready(function () {
    if (window.location.hash.indexOf('showModal') !== -1) {
        window.location.hash = ''; // remove the hash
        jQuery('#languagepopup').modal('show');
    }
});
发布评论

评论列表(0)

  1. 暂无评论