Reputation: 179
I have this code that got me stuck for days I don't understand why does it say in my console that my function is not defined here's the code. by the way, i am super new to jQuery I really don't have much knowledge in it so any help? can somebody tell me where I went wrong that it's giving me this error
(ReferenceError: createGraph is not defined)
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="bin/js/raphael.js"></script>
<script src="bin/js/popup.js"></script>
<script src="bin/js/analytics.js"></script>
</head>
<body>
<div class="action-button">
<button id="1" value="1074">Button 1</button>
<button id="2" value="1074">Button 2</button>
</div>
<div id="output"></div>
<script>
/////////////////////////////////////////////////////
// this script will check if the document is ready and will display data for button 1
$(document).ready(function () {
$("#1").trigger("click");
});
///////////////////////////////////////////////////////////////////////////////////////
$("button").click(function () {
var attr = $(this).attr("id");
var val = $(this).val();
$.ajax({
type: "POST",
url: "bin/some.php",
data: { lookbookID: val, type:attr }
}).done(function( html ) {
$("#output").html(attr + ' - ' + val + ' - ' + html );
createGraph();
});
});
</script>
</body>
</html>
and the JS code
window.onload = function () {
function getAnchors(p1x, p1y, p2x, p2y, p3x, p3y) {
var l1 = (p2x - p1x) / 2,
l2 = (p3x - p2x) / 2,
a = Math.atan((p2x - p1x) / Math.abs(p2y - p1y)),
b = Math.atan((p3x - p2x) / Math.abs(p2y - p3y));
a = p1y < p2y ? Math.PI - a : a;
b = p3y < p2y ? Math.PI - b : b;
var alpha = Math.PI / 2 - ((a + b) % (Math.PI * 2)) / 2,
dx1 = l1 * Math.sin(alpha + a),
dy1 = l1 * Math.cos(alpha + a),
dx2 = l2 * Math.sin(alpha + b),
dy2 = l2 * Math.cos(alpha + b);
return {
x1: p2x - dx1,
y1: p2y + dy1,
x2: p2x + dx2,
y2: p2y + dy2
};
}
function createGraph() {
// Grab the data
alert("i made it!");
var labels = [],
data = [];
$("#data tfoot th").each(function () {
labels.push($(this).html());
});
$("#data tbody td").each(function () {
data.push($(this).html());
});
}
};
I updated this sorry I excluded that window.onload
I still get that reference error but if I put the function outside window.onload = function
then my function would work is there any way I can access my function? I don't know what will happen if I remove the said code from windows.onload
I need help again :)
the link for the function just in case it would be of any help https://github.com/ksylvest/raphael-analytics
Upvotes: 1
Views: 5699
Reputation: 21
createGraph is not a global function , Because $() create a new context,createGraph became a local variables,so if you try to call it ,you get an error.
so let it out from $().
Code like Upstairs..
Upvotes: 1
Reputation: 2104
function createGraph() {
// Grab the data
var labels = [],
data = [];
$("#data tfoot th").each(function () {
labels.push($(this).html());
});
$("#data tbody td").each(function () {
data.push($(this).html());
});
//code here i erased so it wont be too long and i just added this function so i could call it anyway this code is actually the code that creates the graph in raphael JS
}
Upvotes: 2