JS相关

Javascript刷新页面的几种方法: 
1 history.go(0) 
2 location.reload() 
3 location=location 
4 location.assign(location) 
5 document.execCommand('Refresh') 
6 window.navigate(location) 
7 location.replace(location) 
8 document.URL=location.href

 

You can add this function to the String prototype:

if(typeofString.prototype.startsWith !='function'){// see below for better implementation!String.prototype.startsWith =function(str){returnthis.indexOf(str)==0;};}

Then you can use it directly on string values:

"Hello World!".startsWith("He");// truevar data ="Hello world";var input ='He';
data.startsWith(input);// true

Edit: Note that I'm checking if the function exists before defining it, that's because in the future, the language might have this strings extras methods defined as built-in functions, and native implementations are always faster and preferred, see the ECMAScript Harmony String Extras proposal.

Edit: As others noted, indexOf will be inefficient for large strings, its complexity is O(N). For a constant-time solution (O(1)), you can use either, substring as @cobbal suggested, or String.prototype.slice, which behaves similarly (note that I don't recommend using the substr, because it's inconsistent between implementations (most notably on JScript) ):

if(typeofString.prototype.startsWith !='function'){String.prototype.startsWith =function(str){returnthis.slice(0, str.length)== str;};}

The difference between substring and slice is basically that slice can take negative indexes, to manipulate characters from the end of the string, for example you could write the counterpart endsWithmethod by:

if(typeofString.prototype.endsWith !='function'){String.prototype.endsWith =function(str){returnthis.slice(-str.length)== str;};}












关于Jquery中ajax方法data参数用法的总结

jquery手册描述:

data 发送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。必须为 Key/Value 格式。如果为数组,jQuery 将自动为不同值对应同一个名称。如 {foo:["bar1", "bar2"]} 转换为 '&foo=bar1&foo=bar2'。

示例:
$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
});
这里data后面跟的参数可以用二种表式:一种是普通url传参的写法一样,还有一种就是写在json数组里,
上面示例data部分也可以这样写:data: {name:"John",location:"Boston"}。这二个用法有什么区别?
今天在开发中发现二者用法的细微差别。第一种我们用url传参,参数里面如果加带"&"这个符号的话,可能参数接收不到或不完整,如“ data: "name=John&location=Boston",”,
如果name的值是"john&smith"这样写可能就会有问题,我们可以用JS里面的encodeURIComponent()方法进行转义,
但如果用data: {name:"John",location:"Boston"}这种方式写的话就不需要进行转义,如果转义的话,接收的将是转义后的字符串

 

posted on 2013-02-21 19:07  善敗將軍  阅读(150)  评论(0编辑  收藏  举报

导航