0

When I open a new site with the selectbox for example site x then the option x is selected like it should but when I return by clicking the arrow in the left corner above I return to site y but option x is still selected.

Can anyone help me?

 <nav id="navigation" class="navigation">
  <a href="index_V1.html">Home</a>
 <select id="Menu">
   <option disabled selected hidden> # </option>
    <option  href="#">#1</option>
    <option href="#">#2</option>
    <option href="#">#3</option>
   <option href="#">#4</option>
 </select>

 <script>
   document.getElementById('Menu').onchange = function() {
     window.location.href = 
     this.children[this.selectedIndex].getAttribute('href');
   }
 </script>

What should I add to the script to avoid that problem_

Thank you

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
G.Don
  • 17
  • 6
  • you could improve your code to describe your problem. Currently its basic/incomplete. – Nikhil Nanjappa May 24 '17 at 08:24
  • I just started new. Could you say me the commands I need to solve that problem, I will learn them and try to apply them – G.Don May 24 '17 at 08:31
  • Sure, you could maybe replicate your problem here [JSFiddle](https://jsfiddle.net/). Create a basic code **replicating your issue**. – Nikhil Nanjappa May 24 '17 at 08:44
  • Oh you mean for your understanding what my problem actually is? I changed things to upload the code here. If you mean that, I could do that and describe my problem better, if this is what you mean – G.Don May 24 '17 at 08:54
  • Ok, new try to describe my problem :) – G.Don May 24 '17 at 10:05
  • Ok, new try to describe my problem :). Here the link: https://jsfiddle.net/ww6rLw7z/7/ , you go to the select-box whose default value is p. then you choose x, then y. It should first open the site which is linked to element x then to element y. i cant simulate that there but when you click on the arrow left (here the picture http://imgur.com/a/XETj7 ) you should return to the site linked to element x and it works but element y is still selected in the select box. Do you understand what I mean? – G.Don May 24 '17 at 10:11
  • Yes, better. You could update your question providing all these info. – Nikhil Nanjappa May 24 '17 at 10:33
  • Can you check [here](https://jsfiddle.net/kainikhil/ww6rLw7z/9/), if this is what you are looking for. **Copy the code and try in your local**. Online editors usually don't allow redirection with Chrome. – Nikhil Nanjappa May 24 '17 at 11:00
  • Now you cannot return anymore. The problem is solved but I want to return back, if the user wants. The user choose for example x and then y and push the return button he should be on site x again. How do I accomplish this? – G.Don May 24 '17 at 12:55
  • Can you try using `assign` method instead - like this `window.location.assign(` instead of `window.location.replace(` – Nikhil Nanjappa May 24 '17 at 12:58
  • Same thing. You go to site x then y and when you return to site x in the scroll box element y is selected instead of element x. – G.Don May 24 '17 at 13:25
  • The "selected" value would not change per page refresh. It will remain the same for all the pages while page loads. I am sure the selected will be y if you open any other page. Perhaps you have given a `selected` attribute to y option in your html – Nikhil Nanjappa May 24 '17 at 13:38
  • Yep, I used the selected element for every site, so that the right one is selected for each site. I didnt know that this is causing the problem? What can I do about it? I mean without selected, it doesnt work that the correct element is shown each site – G.Don May 24 '17 at 13:49
  • Is the selected element same for every site ? – Nikhil Nanjappa May 24 '17 at 14:02
  • No, I changed them in each site, so everytime the correct item is seleceted. I have the feelings that when you return that the site doesn refreshes. When I have the error and refresh the site the correct item is shown – G.Don May 24 '17 at 14:10
  • Yes, hitting the back button **does not refresh** the site. I will provide you with a code to make the browser explicitly find out & refresh on browser back – Nikhil Nanjappa May 24 '17 at 14:24
  • This would be awesome! I appreciate your help :) – G.Don May 24 '17 at 14:31

1 Answers1

0

Have an hidden input on your pages to identify refresh

<input type="hidden" id="refresh" value="no">

Now use JQuery to check its value & correspondingly refresh the site explicitly,

$(document).ready(function(e) {
    var $input = $('#refresh');
    $input.val() == 'yes' ? location.reload(true) : $input.val('yes');
});

So basically first time the value would be no when page loads & then changed to yes, later if visited again yes would trigger a refresh.

Courtesy of this SO

Nikhil Nanjappa
  • 6,454
  • 3
  • 28
  • 44
  • Hey, sorry for disturbing you. I just asked because it it doesnt work for me. – G.Don May 25 '17 at 07:57
  • You could put the HTML part first in the "x" page and Jquery code in a JS file which runs on "x" site. Then you visit "x" then "y" and come back to "x"(via browser back) & check ? – Nikhil Nanjappa May 25 '17 at 08:25
  • It just doesnt reload.. hmm do I have to put the code into a tag? – G.Don May 25 '17 at 13:59
  • Put the JQuery inside your ` – Nikhil Nanjappa May 25 '17 at 14:04
  • Hm.. It just doesnt work. Could you show me on my Example code? The site just doesnt reload – G.Don May 26 '17 at 11:37