Skip to content

Instantly share code, notes, and snippets.

@KrofDrakula
Created February 11, 2011 09:39
Show Gist options
  • Save KrofDrakula/822134 to your computer and use it in GitHub Desktop.
Save KrofDrakula/822134 to your computer and use it in GitHub Desktop.
// jQuery patch for HTML5 elements using innerShiv by https://github.com/andy-clusta
// http://jdbartlett.github.com/innershiv | WTFPL License
window.innerShiv = (function () {
var div, frag,
inaTable = /^<(tbody|tr|td|col|colgroup|thead|tfoot)/i,
remptyTag = /(<([\w:]+)[^>]*?)\/>/g,
rselfClosing = /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i,
fcloseTag = function (all, front, tag) {
return rselfClosing.test(tag) ? all : front + '></' + tag + '>';
};
return function (html, returnFrag) {
if (!div) {
div = document.createElement('div');
frag = document.createDocumentFragment();
/*@cc_on div.style.display = 'none'; @*/
}
html = html.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var tabled = html.match(inaTable), myDiv = div.cloneNode(true);
if (tabled) html = '<table>' + html + '</table>';
/*@cc_on document.body.appendChild(myDiv); @*/
myDiv.innerHTML = html.replace(remptyTag, fcloseTag);
/*@cc_on document.body.removeChild(myDiv); @*/
if (tabled) myDiv = myDiv.getElementsByTagName(tabled[1])[0].parentNode;
if (returnFrag === false) return myDiv.childNodes;
var myFrag = frag.cloneNode(true), i = myDiv.childNodes.length;
while (i--) myFrag.appendChild(myDiv.firstChild);
return myFrag;
}
} ());
(function ($) {
var init = $.fn.init, html = $.fn.html;
// used by Jquery Templates
// tests to see if html passed to constructor
$.fn.init = function (selector, context) {
if ($.browser.msie && typeof selector == 'string' && selector.indexOf('>') != -1 && selector.indexOf('<') != -1) {
return new init(innerShiv(selector, false));
}
return new init(selector, context);
};
// used by Jquery Unobtrusive Ajax in ASP.NET MVC
// only matches <script></script> tags, without @src and @type
$.fn.html = function (value) {
if ($.browser.msie && typeof value === 'string') {
var scriptsRegex = new RegExp('<script>([\\w\\W]*)</script>', 'gim');
var scripts = value.match(scriptsRegex);
var el = html.apply(this, [innerShiv(value, false)]);
for (var i in scripts) {
var js = scriptsRegex.exec(scripts[i]);
if (js != null)
$.globalEval(js[0]);
}
return el;
}
return html.apply(this, arguments);
};
})(jQuery);
@Laz75
Copy link

Laz75 commented Mar 4, 2011

Hello,
I'm using jQuery 1.5.1 and the script works, but I get a "rootjQuery is undefined" error.(I also had to replace the </script> in RegExp with </script> or it wouldn't work at all).

Any possible soutions?

Thanks.

@KrofDrakula
Copy link
Author

Right now, I've reverted to using jQuery 1.4.4; it seems some changes in the 1.5.x branch caused it to malfunction, but haven't gotten around to figuring out what exactly fails with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment