window.onload从外部JS文件失败 [英] window.onload fails from external JS file

查看:107
本文介绍了window.onload从外部JS文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在外部JS库中有这个:


//////////////////////// /

函数addMyEvent(){

var obj;

if(document.attachEvent){

obj = document.getElementsByTagName(''img'');

for(i = 0; i< obj.length; i ++){

obj [i] .attachEvent(' 'ondrag'',noDrag);

}

obj = document.getElementsByTagName(''a'');

for(i = 0; i< obj.length; i ++){

obj [i] .attachEvent(''ondrag'',noDrag);

}

}

}


函数noDrag(){

//杀死拖动事件

返回false;

}

////////////////////////


如果我从关联的网页(没有框架/ IFrame等等)通过以下方式调用它:


< body onload =" addMyEvent();">


它有效。如果我尝试不引人注意的方法并使用< bodyon页面

和...


window.onload = addMyEvent();


...在JS库中失败了。我的语法是否有问题或只能使用''onload''事件来调用页面。


[FWIW,我必须停止拖动图像作为他们的SRC是查询URL和

拖动的图像得到错误的名称''aspscript.jpg''。我提供了一个下载

链接以获得正确的命名]


TIA,


Mark

Hi, I have this in an external JS library:

/////////////////////////
function addMyEvent(){
var obj;
if(document.attachEvent) {
obj = document.getElementsByTagName(''img'');
for (i=0;i<obj.length;i++) {
obj[i].attachEvent(''ondrag'', noDrag);
}
obj = document.getElementsByTagName(''a'');
for (i=0;i<obj.length;i++) {
obj[i].attachEvent(''ondrag'', noDrag);
}
}
}

function noDrag() {
// kill the drag event
return false;
}
////////////////////////

If I call it from the associated web page (no frames/IFrames, etc.
involved) via:

<body onload="addMyEvent();">

It works. If I try the unobtrusive metho and use <bodyon the page
and...

window.onload = addMyEvent();

... in the JS library it fails. Have I got the syntax wrong or can only
the calling page use the ''onload'' event.

[FWIW, I have to stop drags of images as their SRCs are query URLs and
dragged images get wrong names ''aspscript.jpg''. I provide a download
link for correct naming]

TIA,

Mark

推荐答案

Mark Anderson在2007年6月4日下午5:05发表以下内容:


< snip>
Mark Anderson said the following on 6/4/2007 5:05 PM:

<snip>

window.onload = addMyEvent();
window.onload = addMyEvent();



一旦遇到它就执行addMyEvent函数并且

将返回值(如果有的话)赋给window.onload事件处理程序。要

修复它,删除():


window.onload = addMyEvent;


-

Randy

机会有利于准备好的心灵

comp.lang.javascript常见问题 - http://jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/


-----原始消息-----

来自:Randy Webb < Hi ************ @ aol.com>

新闻组:comp.lang.javascript

已发送:星期一,六月04, 2007 10:24 PM

主题:Re:window.onload从外部JS文件失败

----- Original Message -----
From: "Randy Webb" <Hi************@aol.com>
Newsgroups: comp.lang.javascript
Sent: Monday, June 04, 2007 10:24 PM
Subject: Re: window.onload fails from external JS file


Mark Anderson说以下2007年6月4日下午5:05:


< snip>
Mark Anderson said the following on 6/4/2007 5:05 PM:

<snip>

> window.onload = addMyEvent ();
>window.onload = addMyEvent();



一旦遇到它就执行addMyEvent函数并且

将返回值(如果有的话)赋给window.onload事件处理程序。

要修复它,删除():


window.onload = addMyEvent;


That executes the addMyEvent function as soon as it encounters it and
assigns the return value (if any) to the window.onload event handler.
To "fix" it, remove the ():

window.onload = addMyEvent;



实际上,我试过这个语法(没有支架)也没有喜悦。有没有其他库的

,但现有的库也有这个:

///////

//陷阱Safari / Firefox行为

killDrag = {

init:function(){

if(!document.getElementById ||!document.createTextNode){返回;}

//我们只会捕获< aand< imginside ID''imageLoc''

killDrag.n = document.getElementById(''imageLoc '');

if(!killDrag.n){return;}

killDrag.addMyListeners(''mousedown'',killDrag.suppr essDrag,''img' ');

killDrag.addMyListeners(''mousedown'',killDrag.suppr essDrag,''a'');

},

addMyListeners:function(eventName,functionName,ele ments){

var temp = killDrag.n.getElementsByTagName(elements);

for(var i = 0; i< temp.length; i ++){

temp [i] .addEventListener(eventName,functionName,false);

}

},

suppressDrag:function(e){

e.stop传播();

e.preventDefault();

}

};

window.addEventListener(' 'load'',killDrag.init,false);

////////////////


作为IE浏览器不支持这些事件,我认为他们不重要但是他们可能会在IE中抛出错误吗?但是,如果是这样,为什么来自

< bodywork的函数在库中具有相同的功能?我很难过。


Mark

Actually, I''d tried this syntax (no bracket) too and no joy. There are
no other libraries, but the existing library has this as well:
///////
// trap Safari /Firefox behaviour
killDrag={
init:function(){
if(!document.getElementById || !document.createTextNode){return;}
// we''ll only trap <aand <imginside ID ''imageLoc''
killDrag.n=document.getElementById(''imageLoc'');
if(!killDrag.n){return;}
killDrag.addMyListeners(''mousedown'',killDrag.suppr essDrag,''img'');
killDrag.addMyListeners(''mousedown'',killDrag.suppr essDrag,''a'');
},
addMyListeners:function(eventName,functionName,ele ments){
var temp=killDrag.n.getElementsByTagName(elements);
for(var i=0;i<temp.length;i++){
temp[i].addEventListener(eventName,functionName,false);
}
},
suppressDrag:function(e){
e.stopPropagation();
e.preventDefault();
}
};
window.addEventListener(''load'',killDrag.init,false );
////////////////

As IE doesn''t support these events, I assumed they don''t matter but
perhaps they throw an error in IE? However, if so, why does a call from
<bodywork with the same functions in the library? I''m stumped.

Mark


6月4日晚上11点05分,Mark Anderson < m ... @ notmeyeardley.demon.co.uk>

写道:
On Jun 4, 11:05 pm, "Mark Anderson" <m...@notmeyeardley.demon.co.uk>
wrote:

我在外部JS中有这个library:

for(i = 0; i< obj.length; i ++){
Hi, I have this in an external JS library:
for (i=0;i<obj.length;i++) {



你有没有理由要

创建一个名为i的GLOBAL变量?

Is there a reason you want it to
create a GLOBAL variable called i ?


这篇关于window.onload从外部JS文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆