一些比较实用的javascript方法收集,留着有用
动态加载javascript文件 <script type="text/javascript">
//<!--
/*动态加载方法*/
function loadScript(url, callback){
var script = document.createElement("script");
script.type = "text/javascript";
if(script.readyState){
//IE
script.onreadystatechange = function (){
if(script.readyState == "loaded" || script.readyState == 'complete'){
script.onreadystatechange = null;
callback();
}
};
}else{
//其他浏览器
script.onload = function (){
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
};
/*
* 以下代码是先加载jquery-1.5.1.min.js,等待jquery-1.5.1.min.js加载完之后
* 再加载file2.js,以此类推最后加载file3.js
*/
loadScript("js/jquery-1.5.1.min.js", function(){
loadScript("file2.js", function(){
loadScript("file3.js", function(){
alert("All files are loaded!");
});
});
});
//-->
</script> function GetXmlHttpReuqest(){
var xmlHttp=null;
try
{
xmlHttp = new XMLHttpRequest(); //对于firefox chrome等浏览器
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //对于IE浏览器
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xmlHttp = false;
}
}
} return false;
} //js判断浏览器
<script>
window["MzBrowser"]={};
(function(){
if(MzBrowser.platform) return;
var ua = window.navigator.userAgent;
MzBrowser.platform = window.navigator.platform;
MzBrowser.firefox = ua.indexOf("Firefox")>0;
MzBrowser.opera = typeof(window.opera)=="object";
MzBrowser.ie = !MzBrowser.opera && ua.indexOf("MSIE")>0;
MzBrowser.mozilla = window.navigator.product == "Gecko";
MzBrowser.netscape= window.navigator.vendor=="Netscape";
MzBrowser.safari= ua.indexOf("Safari")>-1;
if(MzBrowser.firefox) var re = /Firefox(\s|\/)(\d+(\.\d+)?)/;
else if(MzBrowser.ie) var re = /MSIE( )(\d+(\.\d+)?)/;
else if(MzBrowser.opera) var re = /Opera(\s|\/)(\d+(\.\d+)?)/;
else if(MzBrowser.netscape) var re = /Netscape(\s|\/)(\d+(\.\d+)?)/;
else if(MzBrowser.safari) var re = /Version(\/)(\d+(\.\d+)?)/;
else if(MzBrowser.mozilla) var re = /rv(\:)(\d+(\.\d+)?)/;
if("undefined"!=typeof(re)&&re.test(ua))
MzBrowser.version = parseFloat(RegExp.$2);
})();
function abc()
{
if(MzBrowser.ie)
{
alert('ie');
}
if(MzBrowser.firefox)
{
alert('firefox');
}
alert(MzBrowser.version);
}
abc();
</script>
</head>
<body>
<hr> <script type="text/javascript">
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
document.write("Browser name: "+ browser);
document.write("<br>");
document.write("Browser version: "+ version);
</script>
<hr> <script type="text/javascript">
document.write("Browser: ");
document.write(navigator.appName + "");
document.write("Browserversion: ");
document.write(navigator.appVersion + "");
document.write("Code: ");
document.write(navigator.appCodeName + "");
document.write("Platform: ");
document.write(navigator.platform + "");
document.write("Cookies enabled: ");
document.write(navigator.cookieEnabled + "");
document.write("Browser's user agent header: ");
document.write(navigator.userAgent + "");
</script>
<hr>
<script type="text/javascript">
var x = navigator
document.write("CodeName=" + x.appCodeName);
document.write("<br>");
document.write("MinorVersion=" + x.appMinorVersion);
document.write("<br>");
document.write("Name=" + x.appName);
document.write("<br>");
document.write("Version=" + x.appVersion);
document.write("<br>");
document.write("CookieEnabled=" + x.cookieEnabled);
document.write("<br>");
document.write("CPUClass=" + x.cpuClass);
document.write("<br>");
document.write("OnLine=" + x.onLine);
document.write("<br>");
document.write("Platform=" + x.platform);
document.write("<br>");
document.write("UA=" + x.userAgent);
document.write("<br>");
document.write("BrowserLanguage=" + x.browserLanguage);
document.write("<br>");
document.write("SystemLanguage=" + x.systemLanguage);
document.write("<br>");
document.write("UserLanguage=" + x.userLanguage);
</script>
<hr>
<!---->
<script type="text/javascript">
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version>=4))
{
document.write("Your browser is good enough!");
}else{
document.write("It's time to upgrade your browser!");
}
</script> //始终将divTop置于窗口的顶部
<script type="text/javascript">
<!--
window.onscroll =function(){
var divTop = document.getElementById("divTop");
divTop.style.top = document.body.scrollTop;
}
//-->
</script> //html code
<div id ="divTop" style = "position:absolute; top :0px;right:0px; color:#cccccc; width:100px;height:20px; backgrond-colot:naty;font-size:12px;">divTop</div> <script type="text/javascript">
//<![CDATA[
(function(w){
alert('load start turn.');
})(window); //监听浏览器
var MSG_UNLOAD="确定离开?";
var UnloadConfirm = {}; //启用监听浏览器刷新、关闭的方法
UnloadConfirm.set = function(confirm_msg){
window.onbeforeunload = function(event){
event = event || window.event;
event.returnValue = confirm_msg;
}
} //关闭监听浏览器刷新、关闭的方法
UnloadConfirm.clear = function(){
window.onbeforeunload = function(){};
}
UnloadConfirm.set(MSG_UNLOAD);
//]]-->
</script> //只能输入数字和小数点
function checkkey(value,e){
var key = window.event?e.keyCode:e.which;
if( (key > 95 && key < 106) ||
(key > 47 && key < 60) ||
(key == 110 && value.indexOf(".") < 0 )||
(key == 190 && value.indexOf(".") < 0 )){
}else if(key !=8){
if (window.event) //IE
{
e.returnValue = false; //event.returnValue=false
}
else //Firefox
{
e.preventDefault();
}
}
} /*
* @检查复选框至少选择一项function
* @create 11/29/2010
*/
function check_check(checkname,message,msgid,ML){
var flag;
var checks = document.getElementsByName(checkname);
for(var i=0;i<checks.length;i++){
if(checks[i].checked==true){
flag = true;
return true;
break;
}
}
if(!flag){
//alert(message);
var msgid = document.getElementById(msgid);
var msgObj = document.createElement("div");
msgObj.innerHTML = message;
msgObj.style.marginLeft = ML;
msgObj.style.color = "#f00";
msgid.appendChild(msgObj);
return false;
}
} /**
*iframe自适应高度代码
*
*/
function iFrameHeight(id){
var cwin=document.getElementById(id);
if (document.getElementById)
{
if (cwin && !window.opera){
if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight){
cwin.height = cwin.contentDocument.body.offsetHeight;
}
else if(cwin.Document && cwin.Document.body.scrollHeight){
cwin.height = cwin.Document.body.scrollHeight;
}
}else{
if(cwin.contentWindow.document && cwin.contentWindow.document.body.scrollHeight)
cwin.height = cwin.contentWindow.document.body.scrollHeight;//Opera
}
}
} //Simple closure example
var list = document.getElementById('list').getElementsByTagName('li');
for (var i=0; i<list.length; i++)
{
list[i].onclick = (function(n){
return function (){
var li = this.parentNode.getElementsByTagName('li');
alert(li.length);
alert(n);
}
})(i);
} //common javascript code write copyright
copyright=new Date();
update=copyright.getFullYear();
document.write("© " + update + " Artist Rise, All Rights Reserved."); //JS 控制 随滚动条移动的层
<script language="javascript">
var SP = {}; SP.getId = function(id){
return document.getElementById(id);
}; SP.sCenter = function(ele) {
var docEle = document.documentElement,
docBody = document.body,
Ele = SP.getId(ele),
Top = docEle.scrollTop+docBody.scrollTop+(docEle.clientHeight-Ele.offsetHeight)/2,
Left = docEle.scrollLeft+(docEle.clientWidth-Ele.offsetWidth)/2;
console.log(Ele.offsetHeight+','+docEle.clientHeight);
Ele.style.top = Top+"px";
Ele.style.left = Left+"px";
};
SP.sLeftTop = function(ele){
var docEle = document.documentElement,
docBody = document.body,
Ele = SP.getId(ele),
Top = docEle.scrollTop+docBody.scrollTop,
Left = docEle.scrollLeft;
Ele.style.top = Top+"px";
Ele.style.left = Left+"px";
};
SP.sLeftBottom = function(ele) {
var docEle = document.documentElement,
docBody = document.body,
Ele = SP.getId(ele),
Top = docEle.scrollTop+docBody.scrollTop+docEle.clientHeight-Ele.offsetHeight,
Left = docEle.scrollLeft;
Ele.style.top = Top+"px";
Ele.style.left = Left+"px";
};
SP.sRightTop = function(ele){
var docEle = document.documentElement,
docBody = document.body,
Ele = SP.getId(ele),
Top = docEle.scrollTop+docBody.scrollTop,
Left = docEle.scrollLeft+docEle.clientWidth-Ele.offsetWidth;
Ele.style.top = Top+"px";
Ele.style.left = Left+"px";
};
SP.sRightBottom = function(ele) {
var docEle = document.documentElement,
docBody = document.body,
Ele = SP.getId(ele),
Top = docEle.scrollTop+docBody.scrollTop+docEle.clientHeight-Ele.offsetHeight,
Left = docEle.scrollLeft+docEle.clientWidth-Ele.offsetWidth;
Ele.style.top = Top+"px";
Ele.style.left = Left+"px";
}; //init
function init(){
SP.sCenter('redcell');
SP.sLeftTop('redcell2');
SP.sLeftBottom('redcell3');
}; window.onscroll = init;
window.onresize = init;
window.onload = init;
</script>
一些比较实用的javascript方法收集,留着有用的更多相关文章
- 实现同时提交多个form(基础方法) 收集
实现同时提交多个form(基础方法) 收集 分类: 1.2-JSP 1.3-J2EE 1.1J2se 1.0-Java相关2011-12-01 20:59 1644人阅读 评论(0) 收藏 举报 bu ...
- 12个十分实用的JavaScript小技巧
12个非常实用的JavaScript小技巧 在这篇文章中将给大家分享12个有关于JavaScript的小技巧.这些小技巧可能在你的实际工作中或许能帮助你解决一些问题. 使用!!操作符转换布尔值 有时候 ...
- (C#)asp_net调试错误解决方法收集(1)
(C#)asp_net调试错误解决方法收集(1) 2007-11-2309:20 一.异常详细信息:System.InvalidOperationException:对于不返回任何键列信息的Selec ...
- 33个非常实用的JavaScript一行代码
33个非常实用的JavaScript一行代码 一.日期处理 1. 检察日期是否有效 该方法用于检测给出的日期是否有效: const isDateValid = (...val) => !Numb ...
- 【精心推荐】几款实用的 JavaScript 图形图表库
一款好的图表插件不是那么容易找到的.最近项目里需要实现统计图表功能,所以在网上搜罗了一圈,找到一些不错的图表插件,分享大家.众多周知,图形和图表要比文本更具表现力和说服力.这里给大家精心推荐几款实用的 ...
- 12个实用的 Javascript 奇淫技巧
这里分享12个实用的 Javascript 奇淫技巧.JavaScript自1995年诞生以来已过去了16个年头,如今全世界无数的网页在依靠她完成各种关键任务,JavaScript曾在Tiobe发布的 ...
- 几款实用的 JavaScript 图形图表库
一款好的图表插件不是那么容易找到的.最近项目里需要实现统计图表功能,所以在网上搜罗了一圈,找到一些不错的图表插件,分享大家.众多周知,图形和图表要比文本更具表现力和说服力.这里给大家精心推荐几款实用的 ...
- jQuery基础学习5——JavaScript方法获取页面中的元素
给网页中的所有<p>元素添加onclick事件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...
- ORA-01033: ORACLE initialization or shutdown in progress 实用的处理方法
ORA-01033: ORACLE initialization or shutdown in progress 实用的处理方法,此问题通常是由于电脑非正常关机造成的,我们可以用下面的方法查找出是那个 ...
随机推荐
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
C - 最大连续子序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- Linux prerouting和postrouting的区别
我大概清楚一点就是从内网出去的时候用POSTROUTING进来的时候用PREROUTING,可是做透明代理的时候确是用PREROUTING.这是为什么呢? 回复: sunnygg pre还是post是 ...
- django-cms 代码研究(七)杂七杂八
实体关系图 核心对象: cms_page/cms_placeholder/cms_cmsplugin. page模型类继承关系图 CMSPlugin&Placeholder模型类继承关系图 = ...
- SSDB 数据库如何换用 rocksdb 引擎?
牧童遥指杏花村,一枝红杏出墙来… SSDB 数据库如何换用 rocksdb 引擎? idea's blog 2014-04-12 71 阅读 rocksdb NoSQL SSDB 数据库使用的是 G ...
- http://www.zhihu.com/question/24896283
http://www.zhihu.com/question/24896283 Rix Tox,太不專業了 三百.知乎用户.raintorr 等人赞同 1. 更改变量名的几种方法这种情况下该如何快速选中 ...
- spring中context:property-placeholder/元素
1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,password,driverClass等 b.分布式应用中client端访问server端所用的 ...
- Django之admin界面恢复及添加数据模型
引自:http://fl0wjacky.github.io/jekyll_demo/2014/07/14/Django-admin.html Django之admin界面恢复及添加数据模型 Djang ...
- 【python】异常处理
转自:http://www.cnblogs.com/IPrograming/p/Python_error_handler.html 1. 抛出异常和自定义异常 Python用异常对象(exceptio ...
- HDU1568斐波那契推理
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- eclipse 反编译插件安装
1.下载jad.exe和jadclipse_3.3.0.jar 2.将jad.exe拷贝到%JAVA_HOME%bin目录下,将jadclipse_3.3.0.jar拷贝到eclipse的plug ...