Best Practices for Ajax and dojo.xhr*

 

Like any type of technology, it’s important to keep best practices in mind when creating your Ajax requests:

  1. GET requests should be used for simply retrieving data from the server. POST requests are optimal when sending form data, or large sets of data, to the server.
  2. It’s advised to always provide an error callback function. Don’t assume that your request will be successful.
  3. Use console statements during debugging to ensure the options you’re sending are correct, and that the response you receive in your callback is the correct format.
  4. From a user experience perspective, it’s helpful to provide some type of indicator during Ajax requests so that the user knows something is happening.

 

 

js错误:对象不支持此属性或方法

对象不支持此属性或方法 错误原因:

可能是js的文件名和另外一个文件重复。

也有可能是js里的function和另外一个function名字重复。

也有可能是js里的function和页面的某一元素重名。

解决方法:不要把js函数名和页面元素名命名为相同的名称,每个函数或者每个页面元素有一个唯一的名称或ID

107个常用Javascript语句

 1.document.write( ” “); 输出语句

2.JS中的注释为//

3.传统的HTML文档顺序是:document- >html- >(head,body)

4.一个浏览器窗口中的DOM顺序是:window- >(navigator,screen,history,location,document)

5.得到表单中元素的名称和值:document.getElementById( “表单中元素的ID號 “).name(或value)

6.一个小写转大写的JS: document.getElementById( “output “).value = document.getElementById( “input “).value.toUpperCase();

7.JS中的值类型:String,Number,Boolean,Null,Object,Function

8.JS中的字符型转换成数值型:parseInt(),parseFloat()

9.JS中的数字转换成字符型:( ” ” 变量)

10.JS中的取字符串长度是:(length)

11.JS中的字符与字符相连接使用 號.

12.JS中的比较操作符有:==等于,!=不等于, >, >=, <. <=

13.JS中声明变量使用:var来进行声明

14.JS中的判定语句结构:if(condition){}else{}

15.JS中的循环结构:for([initial expression];[condition];[upadte expression]) {inside loop}

[ad#468*15-w2]
继续阅读…

JS的对话框

JS的对话框

1.警告框(alert):出现一个提示框

2.询问框(prompt):返回输入的值

<script>

var username=prompt(“请输入你的名字:”);

document.write(“你好!”+username);

</script>

3.确认框(confirm):根据不同的选择,返回true/false

<head>

<script>

function confirmit() {

if(confirm(“你确认删除此文件么?”)) {

document.testa.submit();//如果点确认,则提交为true

}

}

</script>

</head>

<body >

<form action=”tt.htm” method=”post” >

<input value=”删除文件”/>

</form>

</body>

[ad#468*15-w2]

继续阅读…

JS函数及事件处理

JS函数及事件处理:

无参函数定义

<Script language=”JavaScript”>
function test()
{
  document.write(“a”);
}
</Script>
调用:
<Script language=”JavaScript”>
test();
</Script>

有参函数

function test(a)
{
  document.write(a);
}
test(“sldfsf”);–传什么就打印什么

可以返回值,但不需要写返回类型(因为弱类型)\
function test(y)
{
  return y*3;
}
document.write(test(22));

JS事件处理(重点)

1.onFocus:在用户为了输入而选择select,text.textarea等时
<body>
<form name=”test”>
 <input type=”text” name=”userName” value=”sss” onfocus=”JavaScript:alert(document.test.userName.value);” />
</form>>
</body>

2.onBlur:在select ,text,password,textarea失去焦点时
3.onChange:在select,text.textarea的值被改变且失去焦点时
4.onClick:在一个对象被鼠标点中时(button,checkbox,radio,link,reset,submit,text,textarea等)
<body>
<img src=”dsf.jpg” onclick=”alert(‘OK’);” ></img>
</body>
5.onLoad:出现在一个文档完成对一个窗口的载入时(一般写在Body标签中,即窗口第一次载入时)
<body onload=”javacript:alert(‘hello’);” onunload=”javascipt:alert(‘byebye’);”>
nihao
</body>
6.onUnload:当用户退出一个文档时(同上)

 
7.onMouseOver:鼠标被移动到一个对象上时
<body >
<img src=”sdf.jpg” onmouseover=”alert(‘over’);” onmouseout=”alert(‘out’)” ></img>
</body>
8.onMouseOut:鼠标从一个对象移开时(同上)

 
9.onSelect:当form对象中的内容被选中时(比如全选,部分选中等)

 
<body >
<form name=”test”>
 <input type=”text” name=”t” value=”hello” onselect=”javascript:alert(”);” />
    <select name=”se”>
     <option value=”0″>hehe</option>
        <option value=”1″>hehe</option>
    </select>
</form>
</body>
10.onSubmit:出现在用户通过提交按钮提交一个表单时

 
<body >
<form name=”test” action=”1.htm” onsubmit=”return true;”>
 <input type=”submit” value=”ok” />
</form>
</body>
———-
<body >
<form name=”test” action=”1.htm” onsubmit=”alert(‘ok’);”>–在点了提交按钮后但在表单真正提交上去之前弹出
 <input type=”submit” value=”ok” />
</form>
</body>
—–
<body >
<form name=”test” action=”1.htm” onsubmit=”return false;”>–程序执行到此中止
 <input type=”submit” value=”ok” />
</form>
</body>
小结:onsubmit=”return false/true;”很重要,可以用来做验证,注意上面的返回只能是T/F之一


<head>
<script type=”text/javascript”>
 function check() {
  if(document.test.t.value==”") {
   alert(“空串不允许”);
   return false;
  }
  return true;
 }
</script>

</head>
<body >
<form name=”test” action=”1.htm” onsubmit=”return check();”>
 <input type=”text” name=”t” />
 <input type=”submit” value=”ok” />
</form>
</body>

JS基本语法

JS基本语法:

变量

  JS是一门弱类型的语法,所有的变量定义均以var来实现

  JS的变量建议先定义,再使用,最好加注释

  Js区分大小写

  JS可以不需定义即可直接使用变量,但不建议这么做.

数组定义:

  var arr=new Array(3);

    通过arr.length取得数组的长度

注意.JS中数组长度虽然可以定义,但没什么用,不会出现数组溢出的情况

使用+进行字符串连接

字符串在Js也是当成对象使用的

substring(),charAt()

JavaScript初步及调试

JS初步及调试:

JavaScript(ECMAScript)包含三部分内容:

1.基础语法

2.Dom

  Document object model

3.Bom

  Brower object model

 把整个浏览器当作对象,然后就可以通过它控制里面的各个对象,比如工具栏等

alert(“ddd”);

document.write(“ddd”);

引入外部Js(2.js)文件:

<script src=”2.js”></script>

Js弱类型,全部变量使用var来定义

Js的调试:

1.使用打印

2.启用脚本错误通知

IE–工具–internet选项–高级–(取消)禁用脚本调试–打勾禁用脚本调试(其他)–勾上显示每个脚本错误的通知

字体大小: 减小一号 减小一号
▲ Back to top 繁體