Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdown redirect with cookie and ability to being changed

I have searched many topics but I did not find the exact solution for my problem.

I need to create a dropdown which will be placed at whole website because you always can change your location to another city to see the offer. I would like to create a dropdown where customer selects his city for a local delivery. This dropdown will be placed at the intro page and at everypage with offer. After user selects the city then a cookie will be created and user will be redirected to page he selects. The city he selects should always remembered.

What is my problem? Let me explain.

Now when I select the city and for example its New York and my dropdown is located at New Yorks page aswel for city change I keep being redirected and it never ends. So I get infinity redirects.

Second thing which curently works. When you visit the main url adress for example --> example.com it should redirect you to your city if you have chosen your city before. So if you have selected New york before it should redirect you to example.com/kategoria-produktu/cadca/

To sum up: Everything works great but when this dropdown is located at the whole site I keep being redirected at the page I have selected and that is what I need to eliminate.

Any ideas please?

My code is here

<script type="text/javascript">
if (localStorage && localStorage.country) {
    location = localStorage.country;    
}

function formChanged(form) {
    var val = form.options[form.selectedIndex].value;
    if (val !== 'non-value') {
      if (localStorage) {
        localStorage.country = val;
      }
      location = val;
    }
}
</script>

<FORM NAME="form1">
<select onchange="formChanged(this);" NAME="country" SIZE="1">
<OPTION VALUE="non-value">Select delivery city
<OPTION VALUE="/kategoria-produktu/cadca/">New York
<OPTION VALUE="/kategoria-produktu/brno/">Los Angeles
<OPTION VALUE="/kategoria-produktu/bratislava/">Tokyo
</select>
</FORM>

Thanks

like image 829
Lubo Masura Avatar asked Nov 09 '20 19:11

Lubo Masura


Video Answer


1 Answers

Solved by editing code

<script type="text/javascript">
if(location.href.indexOf(localStorage.country) == -1){
location.href = localStorage.country
}

function formChanged(form) {
var val = form.options[form.selectedIndex].value;
if (val !== 'non-value') {
if (localStorage) {
localStorage.country = val;
}

if (!location.href.indexOf(val)) {    
location = val;
}
}
}

</script>
like image 158
Lubo Masura Avatar answered Sep 19 '22 12:09

Lubo Masura