Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foundation 5 Upgrade: "Layer must be a document node"

I'm trying to upgrade my Ruby on Rails app from Foundation 4 to the newly-released Foundation 5, and the CSS switch is going relatively smoothly. However, I'm running into a problem switching the Javascript files.

When I switch out the Foundation 4 versions of the foundation.min.js and modernizr.js files and then reload a page, I get this weird JS error in the console:

Uncaught TypeError: Layer must be a document node foundation.js?body=1:35
  FastClick foundation.js?body=1:35
  FastClick.attach foundation.js?body=1:35
  (anonymous function) foundation.js?body=1:40
  (anonymous function)

I don't even know what FastClick does, but it appears to be included in Foundation 5 and it's stopping Foundation from loading. That, in turn, is causing all of my Foundation-dependent JS to fail as well.

Any help would be appreciated, thank you!

like image 854
Dylan Avatar asked Nov 22 '13 08:11

Dylan


2 Answers

Update: v5.0.3 has been released, which resolves this issue. Be sure to update your foundation-rails gem accordingly.

This was a bug that was resolved in a commit that was merged into v5.0.3. This fix simply wraps an embedded library, FastClick, in jQuery's #ready(). You can solve this by including that version manually until v5.0.3 is released.

By moving the scripts to the bottom of the page, you're giving up Rails Turbolinks. Turbolinks requires that JavaScript is included in the <head /> since it wipes the <body /> with each request.

What's Going On

Foundation no longer waits for the DOM to load and instead executes immediately. For increased mobile performance, Foundation 5 embeds FastClick, a polyfill which removes click delays on browsers with touch UIs. Upon execution, Foundation immediately tries to attach FastClick to the document.body. If Foundation is initialized in the <head />, the body has yet to exist: document.body will reasonably return null causing FastClick to crap its pants.

like image 166
fny Avatar answered Oct 11 '22 13:10

fny


Make sure to have both at the end of your <body> tag, not in the <head>.

This will throw the error:

<html>
<head>
</head>
    <script src="/static/js/modernizr.js"></script>
    <script src="/static/js/foundation.min.js"></script>
<body>
</body>
</html>

But this is going to work:

<html>
<head>
</head>
<body>
    <script src="/static/js/modernizr.js"></script>
    <script src="/static/js/foundation.min.js"></script>
</body>
</html>
like image 26
pistacchio Avatar answered Oct 11 '22 14:10

pistacchio