穆小懒

穆小懒,最可耐


  • Startseite

  • Archiv

js判断浏览器类型的方法总结

Veröffentlicht am 2017-04-06 | in 学习总结 . Jquery |

本方法可判别当前客户端所使用的浏览器是ie,firefox,safari,chrome或者是opera,另外js可以精确到ie浏览器的版本,可直接使用以下代码

第一种方法

1
2
3
4
5
6
7
8
9
10
11
12
13
   var BrowSer = new Object();
Browser.isMozilla = (typeof document.implementation!='undefined')&&(typeof document.implementation.createDocument!='undefined')&&(typeof HTMLDocument!='undefined');
Browser.isIE = window.ActiveObject?true:false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari")!=-1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);
function check(){
alert(Browser.idIE?'ie':'not ie');
alert(Browser.isFirefox?'Firefox':'not Firefox');
alert(Browser.isSafari?'Safari':'not Safari');
alert(Browser.isOpera?'Opera':'not Opera');
}
window.onload = check;

第二种方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function ifBrowser(){
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
var s;
(s = ua.match(/msie([\d.]+)/))?Sys.ie = s[1]:
(s = ua.match(/firefox\/([\d.]+)/))?Sys.chrome = s[1]:
(s = ua.match(/opera.([\d.]+)/))?Sys.opera = s[1]:
(s = ua.match(/version\/([\d.]).*safari/))?Sys.safari = s[1]:0;
if(Sys.ie){//判断是否为IE浏览器
alert(Sys.ie);
if(Sys.ie =='9.0'){//判断是否为IE9
alert('当前版本为ie9.0');
}else if(Sys.ie =='8.0'){
alert('当前版本为ie8.0');
}
}
if(Sys.firefox){//判断是否为火狐浏览器
alert(Sys.firefox);
}
if(Sys.chrome){//Js判断为谷歌chrome浏览器
alert(Sys.chrome);
}
if(Sys.opera){//Js判断为opera浏览器
alert(Sys.opera);
}
if(Sys.safari){//Js判断为苹果safari浏览器
alert(Ss.safari);
}
}

判断是移动端还是pc端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//判断是浏览器还是移动端

function IsPC(){
var userAgentInfo = navigator.userAgent;
var Agents = new Array("Android","iphone","SymbianOS","windows Phone","iPad","iPod");
var flag = true;
for(var v=0;v<Agents.length;V++){
if(userAgentInfo.indexOf(Agents[v])>0){
flag = false;
break;
}
}
return flag;
}
if(!IsPC()){
alert("这是移动端");
}else{
alert("这是pc");
};

jquery刷新页面及js的基本函数

Veröffentlicht am 2017-02-06 | in 学习总结 . JS |

window.location.reload();刷新当前页面

parent.location.reload();刷新父级对象(用于框架)

opener.location.reload();刷新父窗口对象(用于单开窗口)

top.location.reload();刷新最顶端对象(用于多开窗口)

下面介绍一些javascript基本函数

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

2.传统的html文档顺序是:document-》html-》(head,body)

3.浏览器窗口中的DOM顺序是:window-》(navigator,screen,history,location,document)

4.小写转大写的js:document.getElementById(“output”).value = document.getElementById(“input”).value.toUpperCase();

5.js的数据类型:string,number,Boolean,null,object,function

6.js的字符串转成数字:parseInt(),parseFloat();

7.数字转为字符串:(“”+变量)

8.循环的终止命令是break

9.窗口:打开窗口window.open(),关闭窗口:window.close(),窗口本身:self

10.状态栏的设置:window.status=”字符”;

11.弹出信息提示:window.alert(“字符”);

12.弹出确认框:window.confirm();

13.弹出输入提示框:window.prompt();

14.指定当前显示连接的位置:window.location.href = “URL”;

15.取出窗体中的所有表单的数量:document.forms.length;

16.关闭文档的输出流:document.close();

17.创建一个dom元素:document.createElement(),document.createTextNode();

18.指定在不支持脚本的浏览器显示的html:

19.当超链接和onclick事件都有时,则老版本的浏览器专向a.html,否则转向b.html,例如:

20.js的内建对象:有Array,Boolean,Date,Error,EvalError,Function,Math,Number,Object,RegExp,string,SyntaxError,TypeError,URLError

21.js中的换行:/n;

22.窗口全屏大小:

1
2
3
4
5
6
7
8
  <script>
function fullScreen(){
this.moveTo(0,0);
this.outerWidth = screen.availWidth;
this.outerHeight = screen.availHeight;
}
window.maximize = fullScreen;
</script>

23.防止用户对文本框中输入文本:onfocus = “this.blur()”

24.取出某一元素在页面上出现的数量:document.all.tags(“div(或其他元素名)”).length;

25.js中分别为两种窗体输出:模态和非模态。window.showModelDialog(),window.showModeless();

26.状态栏文字的设置:window.status = ‘文字’,默认的状态栏文字设置:window.defaultStatus = ‘文字’;

27.添加到收藏夹:external.addFavorite(“http://www.google.com","google“);

28.js遇到错误脚本时不做任何操作:window.onerror = doNothing;指定错误句柄的语法为:window.onerror = handleError;

29.js中制定当前打开窗口的父窗口:window.opener,支持opener.opener…的多重继续

30.js的self指的是当前窗口

31.js中的top指的是框架集中最顶层的框架

32.js的窗口重定向:window.navigate(“http://www.google.com“);

33.js中的打印:window.print();

34.js中的提示输入框:window.prompt(“message”,”defaultReply”);

35.js中的窗口滚动条:window.scroll(x,y);

36.js中的窗口滚动位置:window.scrollby

37.js中的设置时间间隔:setInterval(“expr”,msecDelay)或setInterval(functionRef,mseDelay)或setTimeout;

38.js的模态显示在IE4+行,在NN行中不行:showModelDialog(“URL”,[arguments],[features]);

39.js中的退出之前使用的句柄:

1
2
3
4
function verfyClose(){
event.returnValue = "we really like you and hope you will stay longer";
}
window.onbeforeunload = verifyClose;

40.当窗体第一次调用时使用onload();

41.当窗体关闭时调用的文件句柄:onunload();

42.window.location的属性:protocol(http),hostname(www.example.com),port(80),host(www.example.com:80),pathname(),hash(“#giantGizmo”,指跳转到相应的锚记),href(全部的信息);

43.window.location.reload()刷新当前页面;

44.parent.location.reload()刷新父级对象(用于框架)

45.opener.location.reload()刷新父窗口对象(用于单开窗口)

46.top.location.reload()刷新最顶端对象(用于多开窗口)

47.window.history.back()返回上一页,window.history.forward()返回下一页,window.history.go(返回第几页,也可以使用访问过的URL)

48.document.write()不换行的输出,document.writeIn()换行输出

49.document.body.noWrap = true; 防止链接文字折行

50.变量名.charAt(第几位),取该变量的第几位字符;

51.”abc”.charCodeAt(第几个),返回第几个字符的ASCII码值;

52.字符串连接:string.concat(sring2),或用+=进行连接

53.变量.indexOf(“字符”,起始位置),返回第一个出现的位置(从0开始计算)

54.string.lastIndexOf(searchString,startIndex)最后一次出现的位置

55.string.match(regExpression),判断字符是否匹配

56.string.replace(regExpression,replaceString)替换现有字符串

57.string.split(分隔符),返回一个数组存储值

58.string.substring(start,length)取从第几位到指定长度的字符串

59.parseInt(string)强制转化成整数

60.parseFloat()强制转换成浮点型,有小数点

123

muxiaolan

我是一个可耐的慕小懒

12 Artikel
11 Kategorien
15 Tags
© 2026 muxiaolan
Erstellt mit Hexo
|
Theme — NexT.Muse v5.1.4