Just copy and paste random bits from stack overflow until it works. This is how 99% of javascript is coded.
How do I check for undefined in ES5 compatible way?
How do I check if I need a polyfill for cookie API?
The accepted answers for both were rediculous hacks because apparently there's no good and easy way to do it