Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inform if Javascript is disabled in the browser [duplicate]

Tags:

javascript

I want to inform a user if javascript is disabled in the browser and tries to access my website. My information will be like: "Please enable javascript in your browser for better use of the website". So how to do this?

like image 414
ANP Avatar asked Jul 16 '10 06:07

ANP


People also ask

How can I tell if JavaScript is disabled?

To detect if JavaScript is disabled in a web browser, use the <noscript> tag. The HTML <noscript> tag is used to handle the browsers, which do recognize <script> tag but do not support scripting. This tag is used to display an alternate text message.

Can a user disabled JavaScript in the browser?

Open Chrome DevTools. Press Control+Shift+P or Command+Shift+P (Mac) to open the Command Menu. Start typing javascript , select Disable JavaScript, and then press Enter to run the command. JavaScript is now disabled.

How do I disable JavaScript If a website is disabled?

The easy and simple solution is using the HTML “noscript” tag to display different content if JavaScript is disabled. See following full “noscript” example : If JavaScript is disabled : Display warning message and hide the entire content with CSS.


2 Answers

If informing your users to enable it is all you want to do, you don't need to depend on server-side code (in fact, I don't think you can). Just use a <noscript> element:

<noscript><p>Please enable JavaScript in your browser for better use of the website.</p></noscript>
like image 78
BoltClock Avatar answered Oct 19 '22 09:10

BoltClock


Well there's the tag for displaying html only without javascript, but if you want to control the CSS depending on it the best thing to do is to then specify .no-js before any CSS that's only for if JS is disabled, then as your first meaningful line of javascript remove that class from the body (and possibly add a .with-js class to apply js-only styles).

You can also have no default class but add one only with JS, it comes down to personal preference though you can/do get a slight flash of content intended for no javascript with the former before the body class is removed.

like image 41
46bit Avatar answered Oct 19 '22 08:10

46bit