I’m trying to wrap everything inside a page’s <body> tag in a div called bodySemex. For most pages, this works fine:
$('body').children().wrapAll('<div id="bodySemex" />')
However, on some pages, this causes the body element to disappear altogether. (For example, avc.com and http://www.theatlantic.com/technology/archive/12/07/how-the-gorgeous-sometimes-fictional-sound-of-the-olympics-gets-made/259742/ both disappear as a result of this line of code.
I get an error message each time this happens; the message seems to indicate that jQuery’s each() function is trying to operate on something that’s undefined.
Uncaught ReferenceError: _gat is not defined
(anonymous function)
e.extend.globalEval chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/jquery.js:2
e.extend.globalEval chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/jquery.js:2
bp chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/jquery.js:2
e.extend.each chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/jquery.js:2
f.fn.extend.domManip chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/jquery.js:4
f.fn.extend.prepend chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/jquery.js:3
f.each.f.fn.(anonymous function) chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/jquery.js:4
(anonymous function) chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/distillery.js:365
e.extend.each chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/jquery.js:2
e.fn.e.each chrome-extension://clcjngnadninklpbdkjccpoenjdomnid/js/jquery.js:2
bodySwap (Note, this is the name of the line of code the this question is about)
I have searched SO for similar issues and tried some alternatives to wrapAll (for example, the alternatives discussed here: Jquery wrapAllInner() ? — Or wrapAll() applied interally? )
Thanks for any help you can provide!
I found a solution that works for me. It appears that the
.each()function was operating on<script>elements, causing it to look for attributes that weren’t defined.So now, I remove all
scriptelements before callingwrapAll(), thusly: