Maison > Article > interface Web > 这两个js有冲突吗?
<code class="language-js"><span class="kd">function</span> <span class="nx">addLoadHandler</span><span class="p">(</span><span class="nx">handler</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">old</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">onload</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nb">window</span><span class="p">.</span><span class="nx">onload</span> <span class="o">===</span> <span class="s1">'function'</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">window</span><span class="p">.</span><span class="nx">onload</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="nx">old</span><span class="p">();</span>
<span class="nx">handler</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nb">window</span><span class="p">.</span><span class="nx">onload</span> <span class="o">=</span> <span class="nx">handler</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</code>
多补点基础知识,多搜索,不要随便提问。你给 onload 赋值了 2 次,前次的值怎么可能还在?<code class="language-js"><span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">a</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
<span class="nx">alert</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span>
</code>
window.onload = Fun1;<code class="language-text"> function addOnloader(newFunc) {
if (typeof window.onload !== 'function') {
window.onload = newFunc;
} else {
var previousOnload = window.onload;
window.onload = function() {
previousOnload();
newFunc();
};
}
}
window.util.addOnloader = addOnloader;
</code>
只能有一个onload
我也来回答一下吧。问题很小白,但是我也是刚从小白出来的(想到自己的痛处,心软了...)。前面的如 @顾轶灵 和@Boring(@不到)说的很明白也很清楚,但是对于小白还是费解了一点。<code class="language-js"><span class="nb">window</span><span class="p">.</span><span class="nx">onload</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span>
<span class="nx">a</span><span class="p">();</span>
<span class="nx">b</span><span class="p">();</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">a</span><span class="p">(){</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'a'</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">function</span> <span class="nx">b</span><span class="p">(){</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'b'</span><span class="p">)</span>
<span class="p">}</span>
</code>
你可以看下事件。直接使用onload,on等等的话后面定义会将前面覆盖,就像顾轶灵说的一样:a=1;a=2。所以事件绑定一般使用事件监听addeventlisten函数,这样就不会被覆盖。