Can't load Raphael.js library, appendChild method not found?
I'm trying to load raphael.js (downloaded and run locally) into an HTML file but the script refuses to exit, erroring out with this in my JS console:
Uncaught TypeError:
Cannot call m开发者_开发百科ethod 'appendChild' of null
bV on raphael.js:7
a on raphael.js:7
(anonymous function) on raphael.html:22
This is for the minified version, the same error occurs in the non-min version on line 1789.
I downloaded the code from the website, tried both compressed and uncompressed, as well as downloading the JS file linked in one of the demos, all of which work fine in my browser (chrome).
Any thoughts?
I was having the same problem and it turned out to be a load order issue. Here's the original code:
<!doctype html>
<html>
<head>
<script src='raphael-1.5.2.js'></script>
<script>
var paper = Raphael(10, 50, 300, 250);
var circle = paper.circle(50, 40, 10);
circle.attr('fill', '#c00');
circle.attr('stroke', '#fff');
</script>
</head>
<body></body>
</html>
Turns out that when Raphael(10, 50, 300, 250)
is called, it tries to append a canvas element to the body ... which doesn't exist yet. So wrapping it in a window.onload
or jQuery onload function solved the problem:
<!doctype html>
<html>
<head>
<script src='raphael-1.5.2.js'></script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script>
<script>
$(function () {
var paper = Raphael(10, 50, 300, 250);
var circle = paper.circle(50, 40, 10);
circle.attr('fill', '#c00');
circle.attr('stroke', '#fff');
});
</script>
</head>
<body></body>
</html>
精彩评论