help.js 814 B

1234567891011121314151617181920212223242526272829
  1. var suggest = document.getElementById('suggestion');
  2. var ask = document.getElementById('ask');
  3. var lastRand = 0;
  4. ask.addEventListener('click', function() {
  5. var rand = Math.floor(suggestions.length * Math.random());
  6. while(rand === lastRand) {
  7. rand = Math.floor(suggestions.length * Math.random());
  8. }
  9. setSuggestion(rand);
  10. });
  11. function setSuggestion(suggestionId) {
  12. if(suggestionId < 0) {
  13. suggestionId = 0;
  14. } else if(suggestionId >= suggestions.length) {
  15. suggestionId = suggestions.length - 1;
  16. }
  17. suggest.innerHTML = suggestions[suggestionId];
  18. lastRand = suggestionId;
  19. document.location.hash = "#" + suggestionId;
  20. }
  21. document.addEventListener("DOMContentLoaded", function(event) {
  22. if(window.location.hash == "") {
  23. ask.click();
  24. } else {
  25. setSuggestion(window.location.hash.split('#')[1]);
  26. }
  27. });