Home Page keeps refreshing after onclick event
Reply: 2

Page keeps refreshing after onclick event

Keith Davis
1#
Keith Davis Published in 2018-01-10 14:49:24Z

I have a landing page, where you can select which country you are from(button click), and then be redirected to the appropriate page. It also sets a cookie and remembers your choice the next time you go to the landing page

When you click through to the a page you have chosen, it keeps on refreshing the page...

I cannot understand where the reloading of the page is coming from - but I think it may be from the button click event.

Below is what I am using.

            jQuery(window).load(function () {

                        var url = 'http://expatri8.com/';
                        var UK_page = 'UK';
                        var AUS_page = 'AUS';
                        var NZ_page = 'NZ';
                        var ZA_page = 'ZA';

                        if (Cookies.get('default_page') != null) {
                            if (window.location.href != url + '/' + Cookies.get('default_page')) {
                                window.location.href = url + '/' + Cookies.get('default_page');

                            }
                        }

                      jQuery('#set_UK_butt').click(function () {

                            Cookies.set('default_page', UK_page, {expires: 999});
                    //alert('UK');
                          jQuery(location).attr('href', 'http://expatri8.com/UK')
                   //  window.location.replace= 'http://expatri8.com/UK';

                                });

                        jQuery('#set_AUS_butt').click(function () {
                            Cookies.set('default_page', AUS_page, {expires: 999});
                            alert('AUS');
                        });


                        jQuery('#set_NZ_butt').click(function () {
                            Cookies.set('default_page', NZ_page, {expires: 999});
                            alert('NZ');
                        });


               jQuery('#set_ZA_butt').click(function () {
                            Cookies.set('default_page', ZA_page, {expires: 999});
                            alert('ZA');
                        });

             });

Any help would be much appreciated.

DylanVB
2#
DylanVB Reply to 2018-01-10 14:52:24Z

Prevent the default button click event (sending a post-request) from happening. In your case:

jQuery('#set_ZA_butt').on("click", function (event) {
    event.preventDefault();
    Cookies.set('default_page', ZA_page, {expires: 999});
    alert('ZA');
});
kLabz
3#
kLabz Reply to 2018-01-10 14:55:20Z

Use window.location.pathname to get the part of the url corresponding to the path (without http/https, without domain, without query string and without hash), so you can test it against Cookies.get('default_page')

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306566 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO