3

I need to redirect new visitor to a splash page created with joomla article.

I used this script:

<script type="text/javascript">
    function redirect() {
        var thecookie = readCookie('doRedirect');
        if (!thecookie) {
            window.location = 'prehome';
        }
    }

    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        } else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    window.onload = function() {
        redirect();
        createCookie('doRedirect', 'true', '999');
    }
</script>

I copied this script in theme.php of the template.
It also works if I add the script in a module and publish it.

But for some seconds the homepage is shown before redirecting to the splash page.

Is there a script to prevent the showing the homepage before splash page?

And, is there a script to apply to my .htaccess file?

1 Answer 1

4

It looks like your redirect script is fired on window.onload, and as described in this post, that's after all your content is loaded. That's why you see the home page for a moment before a user is redirected.

Try modifying your script like this, and place it between your <head> tags:

<script type="text/javascript">
    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        } else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    var thecookie = readCookie('doRedirect');
    if (!thecookie) {
        createCookie('doRedirect', 'true', '999');
        window.location = 'prehome';
    }
</script>

Other solutions

You might also consider using a modal popup, some extensions can load the popup once, and set a cookie in order to avoid users to see it again:

  1. NoNumber Modals
    NoNumber has a nice popup extension called Modals, and it has an option to display popup only once (I think you need the pro version for this):

    {modal html=This modal will only show once|openOnce=1}{/modal}
    

    Check out the demo here.

  2. Joomla Content Editor
    The Joomla Content Editor also has a function to display a popup only once. Read more here: https://www.joomlacontenteditor.net/support/tutorials/jcemediabox/item/create-a-media-popup

     <a href="/instructions.html" class="jcepopup autopopup-single" type="text/html" id="autopopup"></a>
    
3
  • I tried your solution and it works better than my script. Thanks very much
    – Joso
    Dec 31, 2015 at 12:44
  • You're very welcome. I'm glad it works. Happy new year :)
    – johanpw
    Dec 31, 2015 at 12:47
  • Happy New Year to you too.
    – Joso
    Dec 31, 2015 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.