读书人

请教这样的一个js类库应该怎么调用

发布时间: 2013-11-29 13:49:33 作者: rapoo

请问这样的一个js类库应该如何调用

/* $ 获取指定对象
@element 对象名
可以使用对象名集合,返回值为对象的集合
如果您使用了 Prototype 类库, 请把该函数注释掉
Sams_object.Get() 中同样实现该函数的所有功能
*/
function $(element)
{
if (arguments.length > 1)
{
for (var i = 0, elements = [], length = arguments.length; i < length; i++)
elements.push($(arguments[i]));
return elements;
}
if (typeof element == 'string')
element = document.getElementById(element);
return element;
}
/// 浏览器相关操作
var Sams_browse =
{
/* 检测浏览信息 */
checkBrowser : function ()
{
this.ver=navigator.appVersion
this.dom=document.getElementById?1:0
this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
this.ie4=(document.all && !this.dom)?1:0;
this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
this.ns4=(document.layers && !this.dom)?1:0;
this.mac=(this.ver.indexOf('Mac') > -1) ?1:0;
this.ope=(navigator.userAgent.indexOf('Opera')>-1);
this.ie=(this.ie6 || this.ie5 || this.ie4)
this.ns=(this.ns4 || this.ns5)
this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns5 || this.ns4 || this.mac || this.ope)
this.nbw=(!this.bw)
return this;
},
/* 设为首页
@url 要设为首页的地址
*/
SetDefault : function ()
{
this.style.behavior='url(#default#homepage)';
this.setHomePage(this.GetUrl());
return false;
},
/* 复制指定URL地址
@Msg 要写入剪贴板的字符集
*/
SetCopy : function (Msg)
{
if(navigator.userAgent.toLowerCase().indexOf('ie') > -1)
{
clipboardData.setData('Text',Msg);
alert ("网址“"+Msg+"”\n已经复制到您的剪贴板中\n您可以使用Ctrl+V快捷键粘贴到需要的地方");
}
else
{
prompt("请复制网站地址:",Msg);
}
},
/* 加入收藏
@site 站点名称
@url 地址
*/
AddBookmark : function (site, url)
{
if(navigator.userAgent.toLowerCase().indexOf('ie') > -1)
{


window.external.addFavorite(url,site)
}
else if (navigator.userAgent.toLowerCase().indexOf('opera') > -1)
{
alert ("请使用Ctrl+T将本页加入收藏夹");
}
else
{
alert ("请使用Ctrl+D将本页加入收藏夹");
}
},
/* 打开Url指定宽度和高度的窗口 */
OpenWindows : function (url,width,height)
{
window.open(url,'newwin','width='+width+',height='+height);
return false;
},
/* 禁止浏览器的Javascript错误提示 */
CloseError : function()
{
window.onerror = function()
{
return true;
};
},
/* 获取浏览器URL */
GetUrl : function()
{
return location.href;
},
/* 获取URL参数 */
GetUrlParam : function()
{
return location.search;
},
/* 获取页面来源 */
GetFrom : function()
{
return document.referrer;
},
/* 获取指定的URL参数值
@name 参数名
*/
Request : function(name)
{
var GetUrl = this.GetUrl();
var Plist = new Array();
if(GetUrl.indexOf('?') > 0)
{
Plist = GetUrl.split('?')[1].split('&');
}
else if(GetUrl.indexOf('#') > 0)
{
Plist = GetUrl.split('#')[1].split('&');
}
if (GetUrl.length > 0)
{
for(var i=0; i<Plist.length; i++)
{
var GetValue = Plist[i].split('=');
if (GetValue[0].toUpperCase() == name.toUpperCase())
{
return GetValue[1];
break;


}
}
return;
}
},
/* 直接将HTML写到新窗口
@title 标题
@msg 内容
*/
Popmsg : function PopIt(title,msg)
{
var popup = window.open('','popDialog','height=500,width=400,scrollbars=yes');
popup.document.write('<html><title>'+title+'</title><style>body{margin:10px;font:13px Arial;}span{text-line:20px;}</style><body><span style=\'font:14px arial;\'>'+msg + '</span></body></html>');
popup.document.close();
}
};


[解决办法]

<script>
Sams_browse.checkBrowser();/*?检测浏览信息?*/
</script>

读书人网 >JavaScript

热点推荐