2

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!

tomtom
  • 99
  • 1
  • 1
  • 10
  • 2
    You need to pass a parameter to the page and read it with JS there. – SLaks Jan 18 '15 at 22:42
  • possible duplicate of [Event when window.location.href changes](http://stackoverflow.com/questions/3522090/event-when-window-location-href-changes) – Etheryte Jan 18 '15 at 22:44
  • Do you mean show the modal when the page you are redirecting to loads? – unobf Jan 18 '15 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 Jan 18 '15 at 22:59
  • @unobf yes, that is my objective – tomtom Jan 18 '15 at 22:59
  • @SLaks how would that look like? – tomtom Jan 18 '15 at 23:01
  • @Bosman can I write an if statement on the redirected page to fire it if its coming per my redirect? sounds very confusing but somehow it must be solved – tomtom Jan 18 '15 at 23:06
  • Yes you can. As @SLaks mentions you can set a value and read it from a function declared in the redirected page. Essentially you can use any cross page persistent store to keep a value that would be read in the redirected page. For example Session, TempData, query string etc. – IsakBosman Jan 18 '15 at 23:10
  • @Bosman , I guess I do understand what you are saying, but the solution of unobf works perfectly for my purpose. Thank you all for your kind help – tomtom Jan 18 '15 at 23:39

1 Answers1

2

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');
    }
});
unobf
  • 7,158
  • 1
  • 23
  • 36
  • Thanks a lot, I do not really understand it completely but it works. – tomtom Jan 18 '15 at 23:20
  • I triggered a click event on file input instead of showing a modal. It is not working. Am I missing something? – Sambit Feb 04 '16 at 06:04
  • @Sambit can you get that click event to work even when you don't redirect? – unobf Feb 04 '16 at 14:55
  • Yes, I get the click event work in the same page `$("#someLink").click(function(){$("#fileInput").trigger("click");});`). The file input is of hidden type. – Sambit Feb 04 '16 at 15:56
  • how can an input be type="file" and type="hidden" at the same time? – unobf Feb 04 '16 at 23:09