Giter VIP home page Giter VIP logo

innershiv's Introduction

HTML5 innerShiv

Even with HTML5shim, this section won’t be styled in IE < 9:


  var s = document.createElement('div');
  s.innerHTML = "<section>Hi!</section>";
  document.body.appendChild(s);

innerShiv is a workaround.

This is the verbose documentation. If you just want the skinny and the script, go here!

The Problem

The HTML5shim fixes styling static HTML5 documents in old IE, but its reach is limited to the document itself. HTML5 elements that are created before they’re appended to the document fall outside its tender and loving embrace. That’s a common use case, and noticeable in libraries like jQuery when you try to .append() or .load() HTML5 content.

A Solution


  innerShiv("<section>html string</section>")

innerShiv is a function which takes your HTML5 string and returns an IE-safe document fragment or element collection shim’d to work with old IE.

On jQuery

When creating a jQuery object from innerShiv, you should pass a second parameter — boolean false — which instructs innerShiv to return an array of elements instead of a document fragment:


  $(innerShiv("<section>html string</section>", false))

This enables you to do further manipulation before inserting your content into the document. If the jQuery/collection return parameter is not passed, you cannot perform additional manipulations in jQuery.

A lot of people ask how to use innerShiv with jQuery’s .load() method. This gist provides an alternative method .loadShiv(), which is based on and acts like jQuery’s .load() but passes incoming HTML through innerShiv before adding anything to the DOM. Download loadShiv separately and add it to your project, then simply call .loadShiv() instead of .load().

If you’re loading an entire content fragment straight into the document (i.e., if you’re .load()ing into an element that’s currently attached the already-shim’d document, and you’re not including a selector in .load()‘s url parameter), you don’t need to use loadShiv or innerShiv. innerShiv is only needed when you’re setting the innerHTML of an element outside the shim’d document, something jQuery has to do when using a selector on loaded content or when loading content into an element that’s not attached to the document.

Please note that, to keep its filesize down, innerShiv doesn’t provide all the robustness of jQuery’s HTML parsing. For example, it strips inline scripts from the HTML without parsing them.

License

This script is completely, totally, and utterly free. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See here for more details.

innershiv's People

Contributors

thegrandpoobah avatar paneidos avatar

Stargazers

 avatar

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.