<<上篇 | 首页 | 下篇>>

js window.onload 加载多个函数和追加函数 - 唔愛吃蘋果 - 博客园

window.onload 不能同时加载多个函数,后面会把前面的覆盖,解决办法:

 

 function addLoadEvent(func) {
  
var oldonload = window.onload;//得到上一个onload事件的函数
  if (typeof window.onload != 'function') {//判断类型是否为'function',注意typeof返回的是字符串
    window.onload = func;
  } 
else {  
    window.onload 
= function() {
      oldonload();
//调用之前覆盖的onload事件的函数---->由于我对js了解不多,这里我暂时理解为通过覆盖onload事件的函数来实现加载多个函数
      func();//调用当前事件函数
    }
  }
}

//(完整示例)使用如下:

function t(){
alert(
"t")
}
function b(){
alert(
"b")
}
function c(){
alert(
"c")
}
 
function addLoadEvent(func) {
  
var oldonload = window.onload;
  
if (typeof window.onload != 'function') {
    window.onload 
= func;
  } 
else {  
    window.onload 
= function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(t);
addLoadEvent(b);
addLoadEvent(c);
//等价于  window.onload =function() { t();  b(); c() ;}

阅读全文……

Auto refreshing properties from property files | Entelligentsia Blog

一个自动重新加载配置文件修改的Properties类:
 
 
class AutoRefreshingProperties extends Properties{
    private long LAST_LOADED = System.currentTimeMillis();
    private List listeners = Collections.synchronizedList(new ArrayList());
    public AutoRefreshingProperties(String absolutePathToPriopertiesFile) throws IOException{
        this(null, absolutePathToPriopertiesFile);
    }
    public AutoRefreshingProperties(Properties defaults, String absolutePathToPriopertiesFile) throws IOException{
        super(defaults);
        load(absolutePathToPriopertiesFile);
    }
    public AutoRefreshingProperties removeListener(FileChangeListener listener){
        listeners.remove(listener);
        return this;
    }
    public AutoRefreshingProperties addListener(FileChangeListener listener){
        listeners.add(listener);
        return this;
    }
    public synchronized void load(String absoluteFilePath) throws IOException {
        InputStream is = new FileInputStream(new File(absoluteFilePath));
        this.load(is);
        LAST_LOADED = System.currentTimeMillis();
        keepUpdated(absoluteFilePath);
    }
    abstract class FileWatcher implements Runnable{}
    private void keepUpdated(final String absoluteFilePath){
        Executors.newSingleThreadExecutor().submit(
                new FileWatcher(){
                    @Override
                    public void run() {
                        try {
                            while(true){
                                if (new File(absoluteFilePath).lastModified() &gt; LAST_LOADED){
                                    InputStream is =  new FileInputStream(new File(absoluteFilePath));
                                    load(is);
                                    LAST_LOADED = System.currentTimeMillis();
                                    notifyListeners();
                                }
                                Thread.yield();
                                Thread.sleep(2000);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
    }
    private void notifyListeners(){
        final Properties properties = new Properties(this);
        for (FileChangeListener aListener : listeners){
            aListener.notifyFileChanged(properties);
        }
    }
}

阅读全文……

Using Multiple "buttons" Elements in IE6

IE6下点击类型为submit的Button,其他未点击的button也会提交,导致某些程序有问题,下面是一种解决办法。

Save this as buttonfix.js

function buttonfix() {
    var buttons = document.getElementsByTagName('button');
    for (var i=0; i<buttons.length; i++) {
        if(buttons[i].onclick) continue;
        
        buttons[i].onclick = function () {
            for(j=0; j<this.form.elements.length; j++)
                if( this.form.elements[j].tagName == 'BUTTON' )
                    this.form.elements[j].disabled = true;
            this.disabled=false;
            this.value = this.attributes.getNamedItem("value").nodeValue ;
        }
    }
}
window.attachEvent("onload", buttonfix);

On each page which you need the fix, include the following:

<!--[if lt IE 7]>
    <script type="text/javascript" src="buttonfix.js"></script>
<![endif]-->

If the problem is also present in IE7, instead use:

<!--[if IE]>
    <script type="text/javascript" src="buttonfix.js"></script>
<![endif]-->

阅读全文……

标签 : ,