Wikipedia:Reference desk/Archives/Computing/2022 August 17

= August 17 =

Need help interpreting JavaScript code
I am currently learning HTML and I was viewing the source code of my website that I had pre-built using a web hosting service. Included in the source code was the following JavaScript:

I don't think there is anything on my website that would require JavaScript and so naturally I am very confused. I am not very proficient at JavaScript and I was wondering if someone more knowledgeable than me could help me to interpret this code.

What does it do? Would my website still work if it was removed? Any comment is greatly appreciated.

--  WongSeo (talk to me!)   23:49, 17 August 2022 (UTC)
 * TLDR: Pretty much harmless, unless it starts messing with Resource Loader or something that isn't supposed to be spitting out JavaScript.
 * WongSeo, I pasted your code into an unminifier. It basically is:
 * A bunch of variable declarations. (I have omitted some unused functions for brevity.)
 * is basically ,
 * is basically ,
 * is basically the  tag of the document
 * is a function:
 * From the code, it looks like it's trying to detect the browser.
 * It initializes an object  to the value
 * It sets the variable  to the User Agent string, which is something like
 * Note: I'm saying "guess" because the user agent string can be faked and is not to be trusted. I actually fake my user agent when connecting to Wikipedia.
 * It then tries a bunch of regexes on the user agent to try to guess if it is Firefox, Microsoft Edge, Safari, Google Chrome (note two regexes), or Internet Explorer.
 * If any of those match, it searches for the first capturing group to guess the version.
 * Then, it tries to guess the operating system name and version.
 * Compatibility with browser styles is guessed and the o object is modified and returned.
 * Then a bunch of unused functions
 * Then,, which is basically a hyped up version of.
 * is basically scrolling to the top of the page.
 * is a very... let's say questionable function. Here's what it does in a nutshell:
 * Blanks all  attributes from iframes after setting their  s to the  s.
 * Attempts to play all paused autoplaying videos.
 * If the first element with a  value of , it focuses the first text area, dropdown, or input inside that form.
 * looks like an attempt to undo those.
 * It then runs  on.
 * It then inserts some weird CSS rules.
 * Some badly designed user agent sniffing: If the OS version is before 11, insert some kind of CSS fix for some kind of browser bug.
 * It sometimes adds  classes, but I'm not sure when.
 * That's pretty much all there is to it!
 * Hope this helps, weeklyd3  ( block &#124; talk &#124; contributions) 05:02, 18 August 2022 (UTC)
 * This is my first time posting at the reference desk so please tell me if I did something wrong. weeklyd3  ( block &#124; talk &#124; contributions) 05:02, 18 August 2022 (UTC)
 * @Weeklyd3, thank you so much! This helps out a lot.  WongSeo (talk to me!)   15:19, 18 August 2022 (UTC)
 * P.S. You did everything correctly.  WongSeo (talk to me!)   15:19, 18 August 2022 (UTC)
 * Actually, I don't think it is harmless. It starts adding a bunch of weird CSS classes based on your screen, which could really screw up your styling if something is special about htem. weeklyd3  ( block &#124; talk &#124; contributions) 22:25, 18 August 2022 (UTC)
 * Actually, I don't think it is harmless. It starts adding a bunch of weird CSS classes based on your screen, which could really screw up your styling if something is special about htem. weeklyd3  ( block &#124; talk &#124; contributions) 22:25, 18 August 2022 (UTC)


 * "I don't think there is anything on my website that would require JavaScript and so naturally I am very confused." Virtually all modern websites use JavaScript. It's standard practice.  A Quest For Knowledge (talk) 09:49, 19 August 2022 (UTC)
 * It's not compulsory and won't help with learning HTML. I use extensions when on a mobile browser to fake my user agent string and to prevent videos from autoplaying, and that's because of widespread excessive use of javascript like the above. Card Zero  (talk) 14:00, 19 August 2022 (UTC)
 * Most websites have some javascript because most websites are based on a small number of templates that use javascript. Big Tech is happy to provide such templates, because it allows Big Tech to do Evil Things. The scripts are rarely useful to website visitors, but the sites are built in such a way that they don't work without. Also considering how often I must install security updates for the javascript engine, I wonder why our politicians have made laws regulating cookies (which are fairly easy to defeat with browser settings), but not against excessive use of scripts. Please note the sarcasm. I don't follow the conspiracy theories, but I do worry that some parties have gained too much power over the internet. PiusImpavidus (talk) 09:20, 20 August 2022 (UTC)
 * To be fair, if you want, say, a reply button that doesn't reload the page, you need Javascript. It does make it possible to manipulate a lot of things, unfortunately including people. Card Zero  (talk) 12:08, 20 August 2022 (UTC)