这是一个相当长的文件,但看到我无法隔离其中的问题,我别无选择,只能将其包含在其中。正确的外部JavaScript文件的语法
在实际文件中提到的问题是,有两个主要部分,一个涉及一对函数的“画布绘制技术”以及一个验证部分。我根本无法让他们都在同一个JS文件中工作,虽然老实说,我很可能最终将他们分开,我仍然想知道我在哪里犯我的错误。
我倒过无数页的JavaScript函数sytax教程。 http://www.w3schools.com/js/js_functions.asp等
不是我认为它很重要,但该文件是“script.php”,以便被我的网络服务器识别,所以我可以更容易地拉动PHP变量以用于画布绘制。再次感谢所有的帮助。
编辑:最后,任何人都可以推荐一个好的IDE做这样的工作在脚本语言/ JavaScript,会告诉我简单的语法错误吗?
编辑2:控制台消息说...
canvas is null
[Break On This Error] if (canvas.getContext){
是,当这样的事情正在发生的其他要求?我认为如果“registeredUsers”那么canvas = null,它将IF设置为false,并且它应该在IF条件内跳过所有内容。
$(document).ready(function(){
<?php
include '../functions.php';
PrintRecentRegistrations();
// this merely prints three variables as follows.
//var oneWeek = 0;
//var oneMonth = 1;
//var oneDay = 1;
?>
var base = 141;
var top = 0;
function GetRelativeSize(a)
{
if (a <= 10)
{
a = a * 2;
a = 140-a;
return a;
}
if (10 < a <= 50)
{
a = 40 + a;
a = 140-a;
return a;
}
else
{
a = 40 + a * .8;
a = 140-a;
return a;
}
}
/***** If I comment out this canvas work, the Validation below works.
If I don't, the canvase works but the Validation doesn't. ******/
var canvas = document.getElementById("registeredUsers");
if (canvas.getContext){
var ctx = canvas.getContext("2d");
var img = new Image();
img.onload = function() {
ctx.drawImage(img, 0, 0);
ctx.beginPath();
ctx.moveTo(52, base);
ctx.lineTo(52, GetRelativeSize(oneDay));
ctx.lineTo(82, GetRelativeSize(oneDay));
ctx.lineTo(82, base);
ctx.moveTo(112, base);
ctx.lineTo(112, GetRelativeSize(oneWeek));
ctx.lineTo(142, GetRelativeSize(oneWeek));
ctx.lineTo(142, base);
ctx.moveTo(172, base);
ctx.lineTo(172, GetRelativeSize(oneMonth));
ctx.lineTo(202, GetRelativeSize(oneMonth));
ctx.lineTo(202, base);
ctx.fillStyle = "#00FF00";
ctx.fill();
ctx.stroke();
}
img.src = "/img/chart-background.png";
};
$('#started-raining').delay(16500).fadeOut('slow', function() {
$('#finished-raining').fadeIn('slow');
})
$(':input:visible:enabled:first').focus();
// validate signup form on keyup and submit
$("#signupForm").validate({
rules: {
firstname: {
required: true,
minlength: 3
},
tosagree: {
required: true,
},
lastname: {
required: true,
minlength: 3
},
username: {
required: true,
minlength: 5
},
password: {
required: true,
minlength: 5
},
phonenumber: {
required: true,
minlength: 10
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
},
email: {
required: true,
email: true
},
topic: {
required: "#newsletter:checked",
minlength: 2
},
agree: "required"
},
messages: {
firstname: {
required: "Required",
minlength: "3 Characters Minimum"
},
phonenumber: {
required: "Required",
minlength: "10 digit numbers only"
},
lastname: {
required: "Required",
minlength: "3 Characters Minimum"
},
tosagree: {
required: "Resistance is futile",
},
username: {
required: "Required",
minlength: "5 Characters Minimum"
},
password: {
required: "Please provide a password",
minlength: "5 Characters Minimum"
},
confirm_password: {
required: "Please provide a password",
minlength: "5 Characters Minimum",
equalTo: "Does not match"
},
email: "Invalid E-mail",
}
})
// propose username by combining first- and lastname
$("#username").focus(function() {
var firstname = $("#firstname").val();
var lastname = $("#lastname").val();
if(firstname && lastname && !this.value) {
this.value = firstname + "." + lastname;
}
});
});
你检查了浏览器的错误控制台,看看它说什么吗? – bcoughlan
是的,它确实将它们分割成单独的文件,然后萤火虫可以帮助你的语法。如果你把它们放到单独的.js文件中,带IDE插件的Eclipse IDE也可以帮助。 – momo
@waitinforatrain甚至没有考虑错误控制台。 :(虽然只是检查过它,它唯一说的是#signupForm不是一个函数(在#signupForm不存在的页面上)并且该画布为空(在我不打印画布的页面上) 。 – Kulingar