0

I've got a page which has a drop-down list which works by when any of the elements on it are clicked runs JS that hides corresponding elements on the page.

I want to label these different instances, i.e. when clicking on FAQ there will be some global element that notes which instance the page is in. Trying localStorage.myvar isn't supported in IE it seems so I've been trying to find a simple workaround that doesn't involve cookies. (since I'm fairly new to HTML & JS)

I did try simply renaming a varible in JS using JQuery, but the variable isn't of a global scope as its inside an onclick function

Would it be a good idea to simply change some hidden HTML or CSS element as a label OR is this bad practice and should fine another solution? Thanks

Ben Gooding
  • 884
  • 9
  • 18
  • 2
    Sounds like a use for the hash value. https://stackoverflow.com/questions/298503/how-can-you-check-for-a-hash-in-a-url-using-javascript – Lain Jul 23 '18 at 12:43
  • maybe "window.myvar = value" ? – Sébastien Palud Jul 23 '18 at 12:45
  • 1
    What ancient version of IE are you running that it does not support local storage? – epascarello Jul 23 '18 at 12:50
  • Have you heard about front-end frameworks like angular or vue.js? Management of html objects from javascript is very easy with vue.js for example. Also your code will be more readable than the other solution. I also does not recommend to use global variables in javascript. You have to write moduler js files. Every page should have its own variables and global ones can be defined in a main.js but they also be a part of object. Not global javascript scope. – ahmet Jul 23 '18 at 12:53
  • Following up on the point from @epascarello, IE supports local storage from 8 onwards https://caniuse.com/#feat=namevalue-storage – OliverRadini Jul 23 '18 at 12:53
  • @epascarello here's some simplified code which doesn't work in IE 11 for me https://gist.github.com/ben-gooding/c9551a39cc68261ee1fea7c1a4ac9779 – Ben Gooding Jul 23 '18 at 13:41
  • @Lain I'll look into that now, thanks! – Ben Gooding Jul 23 '18 at 13:49

0 Answers0