GithubHelp home page GithubHelp logo

has-js's Introduction

has-js

Adds the class "has-js" to the body to prevent fallback content from showing. Removes this class if an error occurs.

Use this class in your CSS to hide fallback elements.

Usage

Add this javascript directly under the opening <body> tag. This will ensure the class is added before the content is shown:

<script>
document.body.className+=' has-js';
var hasjsrm=setTimeout(function(){document.body.className=document.body.className.replace(' has-js','')},5000);
</script>

The first line adds the class "has-js" to the body. The second removes it again after 5 seconds. We will be cancelling this timeout later if there are no errors in our javascript.

At the foot of the page, just before the closing </html> tag, link to your javascript file(s) as usual:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="/script.js"></script>

In your final js file (script.js), add these lines of code to the bottom of the script:

clearTimeout(hasjsrm);
$('body').addClass('has-js');

This removes the timeout (and adds the class again if it was too late).

If you aren't using jQuery on your page then use this code instead:

clearTimeout(hasjsrm);
document.body.className+=' has-js';

The CSS

This should be obvious, but hide any fallback elements in your CSS by setting them to display: none;:

.has-js input[type="submit"] {
	display: none;
}

Why has-js

Progressive enhancement is a great technique, but many websites briefly show the fallback before the javascript has loaded fully. On slow connections this can be particularly noticeable.

By running this javascript inline and before the closing body tag, there is no possibility for fallback content to be shown.

One potential issue could be if has-js works, but for whatever reason the main JS file fails (dropped network connection, parse error etc). We need our websites to be bulletproof. If, after 5 seconds our javascript hasn't loaded, we show the non-javascript controls to the user. If the script was just taking its time, no harm done, we will hide them again.

If 5 seconds isn't long enough for your particular scenario, feel free to make this time longer.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.