在这里记一下,怕以后找不到了
(不断添加中。。。)
1. 获得元素大小和位置
function getElementPosition(element) {
var result = new Object();
result.x = 0;
result.y = 0;
result.width = 0;
result.height = 0;
if (element.offsetParent) {
result.x = element.offsetLeft;
result.y = element.offsetTop;
var parent = element.offsetParent;
while (parent) {
result.x += parent.offsetLeft;
result.y += parent.offsetTop;
var parentTagName = parent.tagName.toLowerCase();
if (parentTagName != "table" &&
parentTagName != "body" &&
parentTagName != "html" &&
parentTagName != "div" &&
parent.clientTop &&
parent.clientLeft) {
result.x += parent.clientLeft;
result.y += parent.clientTop;
}
parent = parent.offsetParent;
}
}
else if (element.left && element.top) {
result.x = element.left;
result.y = element.top;
}
else {
if (element.x) {
result.x = element.x;
}
if (element.y) {
result.y = element.y;
}
}
if (element.offsetWidth && element.offsetHeight) {
result.width = element.offsetWidth;
result.height = element.offsetHeight;
}
else if (element.style && element.style.pixelWidth && element.style.pixelHeight) {
result.width = element.style.pixelWidth;
result.height = element.style.pixelHeight;
}
return result;
}
2. 简单封装的xmlhttp
var HSL = new Object();
HSL.Ajax = new Object();
HSL.Ajax.READY_STATE_UNINITIALIZED = 0;
HSL.Ajax.READY_STATE_LOADING = 1;
HSL.Ajax.READY_STATE_LOADED = 2;
HSL.Ajax.READY_STATE_INTERACTIVE = 3;
HSL.Ajax.READY_STATE_COMPLETE = 4;
HSL.Ajax.Request = function(url, params, oncomplete, onerror)
{
this.url = url;
this.params = params;
this.req = null;
this.oncomplete = oncomplete;
this.onerror = (onerror) ? onerror : this.defaultError;
this.sendRequest(url);
}
HSL.Ajax.Request.prototype =
{
sendRequest : function(url)
{
if(window.XMLHttpRequest)
{
this.req = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
this.req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(this.req)
{
try
{
var sender = this;
this.req.onreadystatechange = function()
{
sender.onReadyState.call(sender);
}
this.params = this.params + "&t=" + (new Date()).getTime();
this.req.open("GET", url + "?" + this.params, true);
this.req.send(null);
}
catch(err)
{
this.onerror.call(this);
}
}
},
onReadyState : function()
{
var req = this.req;
var ready = req.readyState;
if(ready == HSL.Ajax.READY_STATE_COMPLETE)
{
var httpStatus = req.status;
if(httpStatus == 200 || httpStatus == 0)
{
this.oncomplete.call(this, req.responseText);
}
else
{
this.onerror.call(this);
}
}
},
defaultError : function()
{
//alert("Callback error!");
return;
}
}
3.获得url后面带的参数
function getArgs()
{
var args = new Object();
var query = location.search.substring(1);
var pairs = query.split('&');
for(var i=0; i<pairs.length; i++)
{
var pos = pairs[i].indexOf('=');
if(pos == -1)
continue;
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
args[argname] = decodeURIComponent(value);
}
return args;
}
使用:
http://www.test.aspx?id=55&name=test
var args = getArgs();
alert(args["id"]);
在论坛上也看到更加简便的方法,利用正则表达式
function getQueryString(url,key)
{
var reg = new RegExp(".*?"+ key+"=([^&]*)?&.*?$"+"|.*?"+key+"=([^&]*)?$|");
if(reg.exec(url) == url) return "";
return url.replace(reg,"$1$2");
}
var url = document.location.toString();
alert(getQueryString(url,"id"));