2

I have a popover that appears on a page after a user clicks a link. When the user then clicks the button in the popover, it does some Javascript that I need to still happen, and then redirects the user to another page. I'm trying to interrupt that redirect, and have the user go to a separate page, but nothing I'm trying seems to work.

If I do the redirect right away, it works, but the rest of the Javascript I need to run doesn't get executed. The same with preventDefault, that seems to prevent the other Javascript from happening that I need to still happen. I also tried using:

$(".specialImg").click(function () { window.onbeforeunload = function () { window.location.href = "https://example.com"; }; }); 

But the original redirect still happens.

Anyone have any ideas on how I can accomplish this? I have Javascript and jQuery at my disposal. I should mention I'm trying to do this through Optimizely, so I can't edit the code that's executing directly.

2
  • preventDefault just prevents the normal action of user interface elements, it has no effect on Javascript actions. Commented Feb 19, 2016 at 22:36
  • beforeunload isn't allowed to do a redirect. All it can do is return a string, which may be shown in the "Do you really want to leave the page?" prompt. Commented Feb 19, 2016 at 22:37

1 Answer 1

2

You can execute the code you want and in the end redirect to the other page :

$(".specialImg").click(function () { //Do here what you want //Then in the end redirect to new page window.location = "https://example.com"; }); 

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.