我知道有很多类似的问题,但我仍然没有找到解决问题的方法.我正在尝试使用XMLHttpRequest上传文件,因此我开发了以下代码:
var sendFiles = function(url,onload,onerror,file,headers){
var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp'),
upload = xhr.upload;
API.addEvent(xhr,'readystatechange',function(){
if(xhr.readyState==4)
if((xhr.status>=200 && xhr.status<300) || xhr.status==304){
this.response = this.response || this.responseText;
onload.call(xhr);
}else onerror.call(xhr);
});
xhr.open('POST',url,true);
for(var n=0;n<headers.length;n++)
xhr.setRequestHeader(headers[n]);
xhr.send(file);
return xhr;
};
Run Code Online (Sandbox Code Playgroud)
PHP端脚本是:
<?php
header('Content-type: text/html;charset=ISO-8859-1');
$status = 0;
if(@copy($_FILES['file']['tmp_name'],'test\\' . $_FILES['file']['name']))
$status = 1;
else
$err = '0';
echo '{
"status": ' . $status . '
}';
?>;
Run Code Online (Sandbox Code Playgroud)
但是var $ _FILES ['file']似乎是空的,这意味着文件没有被发送到服务器.然后我决定在下面的代码中使用FormData对象
var sendFiles = function(url,onload,onerror,file,headers){
var xhr = …Run Code Online (Sandbox Code Playgroud) 如果条件确定其值,javascript如何?,请参阅此示例:
<script type="text/javascript">
var bar = ("something" == true);
alert(bar); // 1
if ("something") {
alert("hey!"); // 2
}
</script>
Run Code Online (Sandbox Code Playgroud)
为什么我指向// 2而// 1处的'bar'为假?
正如我所看到的,bar值的计算方式与if条件几乎相同,或者不是?
bash中有没有一种方法可以按深度顺序对目录中的文件进行排序,例如,首先打印当前目录中的文件,然后打印子目录或子目录中的文件,依此类推,始终以其深度为准。
当我找到这段代码时,我正在阅读John Resig关于JavaScript Getters和Setters 的文章:
function Field(val){
this._value = val;
}
Field.prototype = {
get value(){
return this._value;
},
set value(val){
this._value = val;
}
};
Run Code Online (Sandbox Code Playgroud)
我已经过测试,它可以与所有主流浏览器完美配合,除了该死的IE,它给了我SCRIPT1003: ':' expected错误.想了一会儿后,我意识到,这个错误是因为IE不能识别的JavaScript getter和setter,所以我认为,get value和set value有一个语法错误.
有没有办法让这个代码跨浏览器?
提前致谢
编辑:
我还试图检查浏览器是否支持getter和setter:
if(window.__lookupSetter){
Field.prototype = {
get value(){
return this._value;
},
set value(val){
this._value = val;
}
};
}else{
Field.prototype = {
value: function(val){
if(val)
return this._value = val;
return this._value;
}
};
}
Run Code Online (Sandbox Code Playgroud)
但在执行代码之前,IE会检查语法错误,并错误地在get和 …
我是javascript的新手,所以我很抱歉,如果这完全是错的,但我现在正在尝试验证表单,并且我有简单的测试功能.我希望函数validateForm()返回所有三个函数checkName(),checkEmail和checkMessage().我有validateForm()函数的方式,它只运行checkName()函数.有任何想法吗?
function checkName(){
var name=document.forms["contactForm"]["Name"].value;
if(name==null || name==""){
$("input#name").css("border-color", "#ff0000");
$("input#name").attr("placeholder","Your name is required");
return false;
}
else {
$("input#name").css("border-color", "#00a8ff");
$("input#name").attr("placeholder","");
return true;
}
}
function checkEmail(){
var email=document.forms["contactForm"]["Email"].value;
if(email==null || email==""){
$("input#email").css("border-color", "#ff0000");
$("input#email").attr("placeholder","Your email is required");
return false;
}
else {
$("input#email").css("border-color", "#00a8ff");
$("input#email").attr("placeholder","");
return true;
}
}
function checkMessage(){
var message=document.forms["contactForm"]["Message"].value;
if(message==null || message==""){
$("textarea#message").css("border-color", "#ff0000");
$("textarea#message").attr("placeholder","Your message is required");
return false;
}
else {
$("textarea#message").css("border-color", "#00a8ff");
$("textarea#message").attr("placeholder","");
return true;
}
}
function validateForm(){
return checkName() …Run Code Online (Sandbox Code Playgroud) 我有一个.js文件,我正在使用NodeJS执行.这是我的文件的内容:
var ctry = "America";
function outer(msg) {
console.log(msg + " " + ctry) ;
var ctry = "Canada" ;
}
outer("God Bless");
Run Code Online (Sandbox Code Playgroud)
当我运行这个文件时,我希望看到"上帝保佑美国"但相反,我看到"上帝保佑未定义".
如果我注释掉内部var ctry = line,我会得到"God Bless America",如果我将内部var ctry =行移到console.log之上,我会得到"God Bless Canada".最后两个案例似乎是合乎逻辑的,但为什么在console.log之后var ctry的定义导致ctry在上面的代码中被设置为underfined?
我想知道Javascript中括号的工作,所以我编写了这段代码来测试:
((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
4+4
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
Run Code Online (Sandbox Code Playgroud)
其中包括:
( x1174
4+4
) x1174
Run Code Online (Sandbox Code Playgroud)
我在Google Chrome 20(Win64)上测试了上面的代码,它给了我正确的答案(8).
但是,如果我尝试相同的代码,但有1175个括号(两边),我得到一个stackoverflow错误.
你可以在JSFiddle中检查这段代码(注意:在JSFiddle中它停止使用1178个括号)
所以,我的问题是:
我有一个像这样的字符串
string text="~aaa~bbb~ccc~bbbddd";
Run Code Online (Sandbox Code Playgroud)
输入值将为:bbb
所以在上面的字符串中我应该删除值“~bbb”
结果字符串应该是
text="~aaa~ccc~bbbddd";
Run Code Online (Sandbox Code Playgroud) 顺便说一句,我已经开始使用PDF.js,这是一项非常出色的工作.
但现在我想在pdf页面上插入一个图像(来自canvas元素).这是我的代码:
var image = myCanvas.getContext('2d').getImageData(0,0,400,300),
doc = new pdf();
doc.setProperties({
title: fileName,
author: 'VirtuaLab®',
creator: 'pdf.js'
});
doc.addPage();
data = doc.output();
Run Code Online (Sandbox Code Playgroud)
但我还没有找到任何关于在PDF.js页面上插入图像的信息.
也许doc.image()还是doc.addImage?
这是代码
obj = {
a: 'some value';
m: function(){
alert(this.a);
}
}
obj.m();
Run Code Online (Sandbox Code Playgroud)
结果是'some value'.
我听说这个关键字引用了函数的所有者是obj.现在的问题是上面代码中函数的所有者是obj这样的,为什么在函数拥有所有者时使用此关键字很重要,上面的代码应该按照以下方式工作
obj = {
a: 'some value';
m: function(){
alert(a);
}
}
obj.m();
Run Code Online (Sandbox Code Playgroud)
哪个不起作用我知道但为什么?