代码改变世界

js杂文

2011-08-15 14:31  呦菜  阅读(242)  评论(0编辑  收藏  举报

刚刚整理文件夹的时候发现的,不知道从哪里得来的,贴出来以后参考:


文章标签 ‘JavaScript’
JavaScript获取系统时间
2010年7月24日 香草吧噗 2 条评论
/*获取系统时间*/
function getSysDate(){
var thetime = new Date();
return getFormat(thetime.getFullYear()) + “-” + getFormat(thetime.getMonth()+1) + “-” + getFormat(thetime.getDate()) + ” ” + getFormat(thetime.getHours()) + “:” + getFormat(thetime.getMinutes()) + “:” + getFormat(thetime.getSeconds());
}

分类: JavaScript 标签: JavaScript
JavaScript转换时间格式
2010年7月24日 香草吧噗 没有评论
/*将2010-1-6 19:8:2的时间格式转换成2010-01-06 19:08:02的时间格式*/
function getFormat(timeformat){
if(String(timeformat).length == 1){
timeformat = “0″ + String(timeformat);
}
return timeformat;
}

分类: JavaScript 标签: JavaScript
最全介绍Document对象内容集合
2010年6月19日 香草吧噗 没有评论
对象属性

document.title //设置文档标题等价于HTML的title标签

document.bgColor //设置页面背景色

document.fgColor //设置前景色(文本颜色)

document.linkColor //未点击过的链接颜色

document.alinkColor //激活链接(焦点在此链接上)的颜色

document.vlinkColor //已点击过的链接颜色

document.URL //设置URL属性从而在同一窗口打开另一网页

document.fileCreatedDate //文件建立日期,只读属性

document.fileModifiedDate //文件修改日期,只读属性

document.fileSize //文件大小,只读属性

document.cookie //设置和读出cookie

document.charset //设置字符集 简体中文:gb2312

———————————————————————

常用对象方法

document.write() //动态向页面写入内容

document.createElement(Tag) //创建一个html标签对象

document.getElementById(ID) //获得指定ID值的对象

document.getElementsByName(Name) //获得指定Name值的对象

document.body.appendChild(oTag)

———————————————————————

body-主体子对象

document.body //指定文档主体的开始和结束等价于body>/body>

document.body.bgColor //设置或获取对象后面的背景颜色

document.body.link //未点击过的链接颜色

document.body.alink //激活链接(焦点在此链接上)的颜色

document.body.vlink //已点击过的链接颜色

document.body.text //文本色

document.body.innerText //设置body>…/body>之间的文本

document.body.innerHTML //设置body>…/body>之间的HTML代码

document.body.topMargin //页面上边距

document.body.leftMargin //页面左边距

document.body.rightMargin //页面右边距

document.body.bottomMargin //页面下边距

document.body.background //                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           背景图片

document.body.appendChild(oTag) //动态生成一个HTML对象

常用对象事件

document.body.onclick=”func()” //鼠标指针单击对象是触发

document.body.onmouseover=”func()” //鼠标指针移到对象时触发

document.body.onmouseout=”func()” //鼠标指针移出对象时触发

———————————————————————

location-位置子对象

document.location.hash // #号后的部分

document.location.host // 域名+端口号

document.location.hostname // 域名

document.location.href // 完整URL

document.location.pathname // 目录部分

document.location.port // 端口号

document.location.protocol // 网络协议(http:)

document.location.search // ?号后的部分

documeny.location.reload() //刷新网页

document.location.reload(URL) //打开新的网页

document.location.assign(URL) //打开新的网页

document.location.replace(URL) //打开新的网页

———————————————————————


selection-选区子对象

document.selection

———————————————————————

images集合(页面中的图象)

a)通过集合引用

document.images //对应页面上的img标签

document.images.length //对应页面上img标签的个数

document.images[0] //第1个img标签

document.images[i] //第i-1个img标签

b)通过nane属性直接引用

img name=”oImage”

document.images.oImage //document.images.name属性

c)引用图片的src属性

document.images.oImage.src //document.images.name属性.src

d)创建一个图象

var oImage

oImage = new Image()

document.images.oImage.src=”1.jpg”

同时在页面上建立一个img /标签与之对应就可以显示

———————————————————————-

forms集合(页面中的表单)

a)通过集合引用

document.forms //对应页面上的form标签

document.forms.length //对应页面上/formform标签的个数

document.forms[0] //第1个/formform标签

document.forms[i] //第i-1个/formform标签

document.forms[i].length //第i-1个/formform中的控件数

document.forms[i].elements[j] //第i-1个/formform中第j-1个控件

b)通过标签name属性直接引用

/formform name=”Myform”>input name=”myctrl”/>/form

document.Myform.myctrl //document.表单名.控件名

c)访问表单的属性

document.forms[i].name //对应form name>属性

document.forms[i].action //对应/formform action>属性

document.forms[i].encoding //对应/formform enctype>属性

document.forms[i].target //对应/formform target>属性

document.forms[i].appendChild(oTag) //动态插入一个控件

document.all.oDiv //引用图层oDiv

document.all.oDiv.style.display=” //图层设置为可视

document                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                .all.oDiv.style.display=”none” //图层设置为隐藏

document.getElementId(”oDiv”) //通过getElementId引用对象

document.getElementId(”oDiv”).style=”

document.getElementId(”oDiv”).display=”none”

/*document.all表示document中所有对象的集合

只有ie支持此属性,因此也用来判断浏览器的种类*/

图层对象的4个属性

document.getElementById(”ID”).innerText //动态输出文本

document.getElementById(”ID”).innerHTML //动态输出HTML

document.getElementById(”ID”).outerText //同innerText

document.getElementById(”ID”).outerHTML //同innerHTML

分类: 编程开发 标签: JavaScript
jQuery实现Ajax的简单示例
2010年6月19日 香草吧噗 没有评论
function weblogin(){
var email = $(‘#reg_email’).val().replace(/\s/g,”");
var psw = $(‘#reg_psw’).val().replace(/\s/g,”");
var remember = $(“#remember”).attr(“checked”);
$.ajax(
{
type: “POST”,
url: ajaxBaseUrl + “/Public/doAjaxLogin”,
processData: false,
timeout: 20000,
error: function(){alert(“unknow error”);},
data: “login_email=” + email + “&login_psw=” + psw + “&remember=” + remember ,
success: function(msg)
{
if(msg.indexOf(“SUCCESS”)>=0){
document.getElementById(“btn_login”).disabled = “”;
}
}
}
);
}

分类: JavaScript 标签: JavaScript, jQuery
JavaScript实现Ajax的简单示例
2010年6月19日 香草吧噗 没有评论
<?php
if($_GET){
for($i=0; $i<10; $i++){
echo $_GET["id"];
}
exit;
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Ajax</title>
<script type=”text/javascript”>
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(url){
createXMLHttpRequest();
xmlHttp.open(“GET”, “?id=” + url, true);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
document.getElementById(“result”).innerHTML = xmlHttp.responseText;
}
}
xmlHttp.send(null);
}
</script>
</head>

<body>
<a href=”javascript:startRequest(‘o’);”>o</a>
<a href=”javascript:startRequest(‘f’);”>f</a>
<div id=”result”></div>
</body>
</html>?

分类: JavaScript 标签: Ajax, JavaScript
javascript获取和设置FCKeditor内容
2010年5月23日 香草吧噗 没有评论
利用Javascript取和设FCKeditor值也是非常容易的,如下:

// 获取编辑器中HTML内容
function getEditorHTMLContents(EditorName) {
var oEditor = FCKeditorAPI.GetIns                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                tance(EditorName);
return(oEditor.GetXHTML(true));
}

// 获取编辑器中文字内容
function getEditorTextContents(EditorName) {
var oEditor = FCKeditorAPI.GetInstance(EditorName);
return(oEditor.EditorDocument.body.innerText);
}

// 设置编辑器中内容
function SetEditorContents(EditorName, ContentStr) {
var oEditor = FCKeditorAPI.GetInstance(EditorName) ;
oEditor.SetHTML(ContentStr) ;
}

FCKeditorAPI是FCKeditor加载后注册的一个全局对象,利用它我们就可以完成对编辑器的各种操作。

在当前页获得 FCK 编辑器实例:
var Editor = FCKeditorAPI.GetInstance(‘InstanceName’);

从 FCK 编辑器的弹出窗口中获得 FCK 编辑器实例:
var Editor = window.parent.InnerDialogLoaded().FCK;

从框架页面的子框架中获得其它子框架的 FCK 编辑器实例:
var Editor = window.FrameName.FCKeditorAPI.GetInstance(‘InstanceName’);

从页面弹出窗口中获得父窗口的 FCK 编辑器实例:
var Editor = opener.FCKeditorAPI.GetInstance(‘InstanceName’);

获得 FCK 编辑器的内容:
oEditor.GetXHTML(formatted); // formatted 为:true|false,表示是否按HTML格式取出
也可用:
oEditor.GetXHTML();

设置 FCK 编辑器的内容:
oEditor.SetHTML(“content”, false); // 第二个参数为:true|false,是否以所见即所得方式设置其内容。此方法常用于”设置初始值”或”表单重置”哦作。

插入内容到 FCK 编辑器:
oEditor.InsertHtml(“html”); // “html”为HTML文本

检查 FCK 编辑器内容是否发生变化:
oEditor.IsDirty();

在 FCK 编辑器之外调用 FCK 编辑器工具条命令:
命令列表如下:
DocProps, Templates, Link, Unlink, Anchor, BulletedList, NumberedList, About, Find, Replace, Image, Flash, SpecialChar, Smiley, Table, TableProp, TableCellProp, UniversalKey, Style, FontName, FontSize, FontFormat, Source, Preview, Save, NewPage, PageBreak, TextColor, BGColor, PasteText, PasteWord, TableInsertRow, TableDeleteRows, TableInsertColumn, TableDeleteColumns, TableInsertCell, TableDeleteCells, TableMergeCells, TableSplitCell, TableDelete, Form, Checkbox, Radio, TextField, Textarea, HiddenField, Button, Select, ImageButton, SpellCheck, FitWindow, Undo, Redo

使用方法如下:
oEditor.Commands.GetCommand(‘FitWindow’).Execute();

= FCKConfig.BasePath + ‘plugins/’
// FCKConfig.Plugins.Add( ‘placeholder’, ‘en,it’ ) ;


去掉//后,就相当于把placeholder这个插件功能加上了,fckeditor的插件文件都在/editor/plugins/文件夹下分类按文 件夹放置的,对于fckeditor2.0来说,里面有两个文件夹,也就是有两个官方插件,placeholder这个文件夹就是我们刚才加上去的,主要 用于多参数或单参数自定义标签的匹配,这个在制作编辑模板时非常管用,要想看具体实例的                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 话,大家可以去下载acms 这个系统查看学习,另一个文件夹tablecommands就是编辑器里的表格编辑用到的了。当然,如果你想制作自己其它用途的插件,那就只要按照 fckeidtor插件的制作规则制作完放置在/editor/plugins/下就行,然后再在fckeidtor.js里再添加 FCKConfig.Plugins.Add(‘Plugin Name’,',lang,lang’);就可以了。

第二部分 ,如何让编辑器一打开的时候,编辑工具条不出现,等点“展开工具栏”时才出现?Easy,FCKeditor本身提供了这个功能啦,打开fckconfig.js,找到

FCKConfig.ToolbarStartExpanded = true ;
改成
FCKConfig.ToolbarStartExpanded = false ;
就可以啦!

第三部分,使用自己的表情图标,同样打开fckcofnig.js到最底部那一段
FCKConfig.SmileyPath = FCKConfig.BasePath + ‘images/smiley/msn/’ ;
FCKConfig.SmileyImages = ['regular_smile.gif','sad_smile.gif','wink_smile.gif'] ;
FCKConfig.SmileyColumns = 8 ;
FCKConfig.SmileyWindowWidth = 320 ;
FCKConfig.SmileyWindowHeight = 240 ;

上面这段已经是我修改过的了,为了我发表此文的版面不会被撑得太开,我把FCKConfig.SmileyImages那一行改得只有三个表情图了。

第一行,当然是表情图标路径的设置,第二行是相关表情图标文件名的一个List,第三行是指弹出的表情添加窗口最每行的表情数,下面两个参数是弹出的模态窗口的宽和高喽。

第四部分,文件上传管理部分

此部分可能是大家最为关心的,上一篇文章简单的讲了如何修改来上传文件以及使用fckeidtor2.0才提供的快速上传功能。再我们继续再深层次的讲解上传功能

FCKConfig.LinkBrowser = true ;
FCKConfig.ImageBrowser = true ;
FCKConfig.FlashBrowser = true ;在fckconfig.js找到这三句,这三句不是连着的哦,只是我把他们集中到这儿来了,设置为true的意思就是允许使用fckeditor来浏览 服务器端的文件图像以及flash等,这个功能是你插入图片时弹出的窗口上那个“浏览服务器”按钮可以体现出来,如果你的编辑器只用来自己用或是只在后台 管理用,这个功能无疑很好用,因为他让你很直观地对服务器的文件进行上传操作。但是如果你的系统要面向前台用户或是像blog这样的系统要用的话,这个安 全隐患可就大了哦。于是我们把其一律设置为false;如下

FCKConfig.LinkBrowser = false ;
FCKConfig.ImageBrowser = false ;
FCKConfig.FlashBrowser = false ;

这样一来,我们就只有快速上传可用了啊,好!接下来就来修改,同样以asp为范例进行,进入/editor/filemanager/upload/asp/打开config.asp,修改
ConfigUserFilesPath = “/UserFiles/”这个设置是上传文件的总目录,我这里就不动了,你想改自己改了

好,再打开此                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                目录下的upload.asp文件,找到下面这一段
Dim resourceType
If ( Request.QueryString(“Type”) <> “” ) Then
resourceType = Request.QueryString(“Type”)
Else
resourceType = “File”
End If
然后再在其后面添加
ConfigUserFilesPath = ConfigUserFilesPath & resourceType &”/”& Year(Date()) &”/”& Month(Date()) &”/”
这样的话,上传的文件就进入“/userfiles/文件类型(如image或file或flash)/年/月/”这样的文件夹下了,这个设置对单用户来用已经足够了,如果你想给多用户系统用,那就这样来改
ConfigUserFilesPath = ConfigUserFilesPath & Session(“username”) & resourceType &”/”& Year(Date()) &”/”& Month(Date()) &”/”
这样上传的文件就进入“/userfiles/用户目录/文件类型/年/月/”下了,当然如果你不想这么安排也可以修改成别的,比如说用户目录再深一层等,这里的Session(“username”)请根据自己的需要进行修改或换掉。

上传的目录设置完了,但是上传程序还不会自己创建这些文件夹,如果不存在的话,上传不会成功的,那么我们就得根据上面的上传路径的要求进行递归来生成目录了。

找到这一段
Dim sServerDir
sServerDir = Server.MapPath( ConfigUserFilesPath )
If ( Right( sServerDir, 1 ) <> “\” ) Then
sServerDir = sServerDir & “\”
End If

把它下面的这两行
Dim oFSO
Set oFSO = Server.CreateObject( “Scripting.FileSystemObject” )
用下面这一段代码来替换
dim arrPath,strTmpPath,intRow
strTmpPath = “”
arrPath = Split(sServerDir, “\”)
Dim oFSO
Set oFSO = Server.CreateObject( “Scripting.FileSystemObject” )
for intRow = 0 to Ubound(arrPath)
strTmpPath = strTmpPath & arrPath(intRow) & “\”
if oFSO.folderExists(strTmpPath)=false then
oFSO.CreateFolder(strTmpPath)
end if
next
用这段代码就可以生成你想要的文件夹了,在上传的时候自动生成。

好了,上传文件的修改到现在可以暂时告一段落了,但是,对于中文用户还存在这么个问题,就是fckeditor的文件上传默认是不改名的,同时还不 支持中文文件名,这样一来是上传的文件会变成“.jpg”这样的无法读的文件,再就是会有重名文件,当然重名这点倒没什么,因为fckeditor会自动 改名,会在文件名后加(1)这样来进行标识。但是,我们通常的习惯是让程序自动生成不重复的文件名

在刚才那一段代码的下面紧接着就是
‘ Get the uploaded file name.
sFileName = oUploader.File( “NewFile” ).Name
看清楚了,这个就是文件名啦,我们来把它改掉,当然得有个生成文件名的函数才行,改成下面这样

‘//取得一个不重复的序号
Public Function GetNewID()
dim ranNum
dim dtNow
randomize
dtNow=Now()
ranNum=int(90000*rnd)+                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               10000
GetNewID=year(dtNow) & right(“0″ & month(dtNow),2) & right(“0″ & day(dtNow),2) & right(“0″ & hour(dtNow),2) & right(“0″ & minute(dtNow),2) & right(“0″ & second(dtNow),2) & ranNum
End Function

‘ Get the uploaded file name.
sFileName = GetNewID() &”.”& split(oUploader.File( “NewFile” ).Name,”.”)(1)

上传的文件就自动改名生成如20050802122536365.jpg这样的文件名了,是由年月日时分秒以及三位随机数组成的文件名了

FCKeditor加载完成后做处理的方法
function FCKeditor_OnComplete( editorInstance )
{
editorInstance.Events.AttachEvent( ‘OnBlur’ , FCKeditor_OnBlur ) ;
editorInstance.Events.AttachEvent( ‘OnFocus’, FCKeditor_OnFocus ) ;
}

function FCKeditor_OnBlur( editorInstance )
{
editorInstance.ToolbarSet.Collapse() ;
}

function FCKeditor_OnFocus( editorInstance )
{
editorInstance.ToolbarSet.Expand() ;
}


分类: 编程开发 标签: FCKeditor, JavaScript
JavaScript获取当前页URL
2010年4月27日 香草吧噗 没有评论
var pageUrl = window.location.toString();

pageUrl即为当前页面的URL

分类: JavaScript 标签: JavaScript, url
js实现页面跳转的几种方式
2010年4月12日 香草吧噗 没有评论
第一种:
<script language=”javascript” type=”text/javascript”>
window.location.href=”login.jsp?backurl=”+window.location.href;
</script>

第二种:
<script language=”javascript”>
alert(“返回”);
window.history.back(-1);
</script>

第三种:
<script language=”javascript”>
window.navigate(“top.jsp”);
</script>

第四种:
<script language=”JavaScript”>
self.location=’top.htm’;
</script>

第五种:
<script language=”javascript”>
alert(“非法访问!”);
top.location=’xx.jsp’;
</script>

分类: 编程开发 标签: JavaScript
javascript自动关闭窗口代码
2010年3月18日 香草吧噗 没有评论
<script language=”javascript”>
<!–
function clock(){i=i-1
document.title=i+”秒后本内容将关闭!”;
if(i>0)setTimeout(“clock();”,1000);
else self.close();}
var i=120
clock();
//–>
</script>

分类: JavaScript 标签: JavaScript
javascript对应php urlencode/urldecode的函数
2010年2月9日 香草吧噗 没有评论
1. function Encode()
2. {
3.     var con=document.getElementById(“content”).value;
4.     document.getElementById(“content”).value=UrlEncode(con);
5. }
6.
7. function UrlEncode(str){
8.    var ret=”";
9.    var strSpecial=”!\”#$%&’()*+,/:;<=>?[]^`{|}~%”;
10.    for(var i=0;i<str.length;i++){
11.    var chr = str.charAt(i);
12.      var c=str2asc(chr);
13.      if(parseInt(“0x”+c) > 0×7f){
14.        ret+=”%”+c.slice(0,2)+”%”+c.slice(-2);
15.      }else{
16.        if(chr==” “)
17.          ret+=”+”;
18.        else if(str                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                Special.indexOf(chr)!=-1)
19.          ret+=”%”+c.toString(16);
20.        else
21.          ret+=chr;
22.      }
23.    }
24.    return ret;
25. }
26.
27. function UnEncode()
28. {
29.    var con=document.getElementById(“content”).value;
30.    document.getElementById(“content”).value=UrlDecode(con);
31. }
32.
33. function UrlDecode(str){
34.    var ret=”";
35.    for(var i=0;i<str.length;i++){
36.    var chr = str.charAt(i);
37.      if(chr == “+”){
38.        ret+=” “;
39.      }else if(chr==”%”){
40.      var asc = str.substring(i+1,i+3);
41.      if(parseInt(“0x”+asc)>0×7f){
42.        ret+=asc2str(parseInt(“0x”+asc+str.substring(i+4,i+6)));
43.        i+=5;
44.      }else{
45.        ret+=asc2str(parseInt(“0x”+asc));
46.        i+=2;
47.      }
48.      }else{
49.        ret+= chr;
50.      }
51.    }
52.    return ret;
53. }