這是一個相當長的文件,但看到我無法隔離其中的問題,我別無選擇,只能將其包含在其中。正確的外部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