Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

URL Redirect with Modal Opening

Anyone know if there is a way to set up a redirect for a URL and than have some Jquery or some code to detect if the user was redirected from that specific URL and then show a modal. Have some code set up already.

<script type="text/javascript">
        jQuery(document).ready(function($) {
              var referrer = document.referrer;
                console.log(referrer);
                if (referrer == "http://somedomain.com") {
                    setTimeout(function() {
                        $('#modalWindow').modal('show');
                    }, 5000);
                }
        });
    </script>

    <!--
SCIPRT THAT NEEDS TO GO ON REDIRECTED SITE

    <script type="text/javascript">
        jQuery(document).ready(function($) {
            window.location.replace("http://redirecttothisdomain.com");
        });
    </script>

    -->

This code works great when someone types it in. But I am getting some errors from users and I know this does not work when someone clicks on a bookmark.

Curious if someone knows of a different or better way to approach this so that no matter what if a user gets redirected from a certain URL they will see the modal.

Just wondering is all this done through the client?

What do you mean by client?

please disregard my question was a little confused. I believe if you replace the

window.location.replace();

with something like:

window.location.href="URL".

this might solve the bookmarking and clicking back problem as this allows the page to be cached before redirection.

Your best bet though would be to serve the file from a server and when the page makes a request to the redirection page it will be served modal and all.