473,574 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

window.onload fails from external JS file

Hi, I have this in an external JS library:

/////////////////////////
function addMyEvent(){
var obj;
if(document.att achEvent) {
obj = document.getEle mentsByTagName( 'img');
for (i=0;i<obj.leng th;i++) {
obj[i].attachEvent('o ndrag', noDrag);
}
obj = document.getEle mentsByTagName( 'a');
for (i=0;i<obj.leng th;i++) {
obj[i].attachEvent('o ndrag', 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="addMyEv ent();">

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
Jun 4 '07 #1
20 11854
Mark Anderson said the following on 6/4/2007 5:05 PM:

<snip>
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;

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 4 '07 #2
----- Original Message -----
From: "Randy Webb" <Hi************ @aol.com>
Newsgroups: comp.lang.javas cript
Sent: Monday, June 04, 2007 10:24 PM
Subject: Re: window.onload fails from external JS file

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

<snip>
>window.onloa d = 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;
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.ge tElementById || !document.creat eTextNode){retu rn;}
// we'll only trap <aand <imginside ID 'imageLoc'
killDrag.n=docu ment.getElement ById('imageLoc' );
if(!killDrag.n) {return;}
killDrag.addMyL isteners('mouse down',killDrag. suppressDrag,'i mg');
killDrag.addMyL isteners('mouse down',killDrag. suppressDrag,'a ');
},
addMyListeners: function(eventN ame,functionNam e,elements){
var temp=killDrag.n .getElementsByT agName(elements );
for(var i=0;i<temp.leng th;i++){
temp[i].addEventListen er(eventName,fu nctionName,fals e);
}
},
suppressDrag:fu nction(e){
e.stopPropagati on();
e.preventDefaul t();
}
};
window.addEvent Listener('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
Jun 4 '07 #3
dd
On Jun 4, 11:05 pm, "Mark Anderson" <m...@notmeyear dley.demon.co.u k>
wrote:
Hi, I have this in an external JS library:
for (i=0;i<obj.leng th;i++) {
Is there a reason you want it to
create a GLOBAL variable called i ?

Jun 5 '07 #4

"dd" <dd****@gmail.c omwrote in message
news:11******** **************@ w5g2000hsg.goog legroups.com...
On Jun 4, 11:05 pm, "Mark Anderson" <m...@notmeyear dley.demon.co.u k>
wrote:
>Hi, I have this in an external JS library:
for (i=0;i<obj.leng th;i++) {

Is there a reason you want it to
create a GLOBAL variable called i ?
Global? The var 'i' isn't explicitly declared but it is inside a
function - how then is it global? Surely its scope is that of the
containing function:

function addMyEvent(){
var obj;
if(document.att achEvent) {
obj = document.getEle mentsByTagName( 'img');
for (i=0;i<obj.leng th;i++) {
obj[i].attachEvent('o ndrag', noDrag);
}
obj = document.getEle mentsByTagName( 'a');
for (i=0;i<obj.leng th;i++) {
obj[i].attachEvent('o ndrag', noDrag);
}
}
}

What would be more helpful at this point would be any pointers on why my
window.onload call in an external JS file fails.

Regards

Mark
Jun 5 '07 #5
Mark Anderson said the following on 6/5/2007 4:04 PM:
"dd" <dd****@gmail.c omwrote in message
news:11******** **************@ w5g2000hsg.goog legroups.com...
>On Jun 4, 11:05 pm, "Mark Anderson" <m...@notmeyear dley.demon.co.u k>
wrote:
>>Hi, I have this in an external JS library:
for (i=0;i<obj.leng th;i++) {
Is there a reason you want it to
create a GLOBAL variable called i ?

Global?
Yes, it is Global.
The var 'i' isn't explicitly declared but it is inside a
function - how then is it global?
Because without the var keyword, it is created as a Global variable.
Surely its scope is that of the containing function:
No, it isn't. And easy enough to test:

function testIt(){
for (i=0;i<1000;i++ ){}
}
testIt()
window.onload=c heckI
function checkI(){
alert(i)
}

What will the alert show? If i isn't Global then it will alert
undefined. If it is Global, then it will alert 1000. Test it :)
What would be more helpful at this point would be any pointers on why my
window.onload call in an external JS file fails.
Start removing other parts of the code until it works. Then start adding
them back until it breaks. That is how you find errors/problems and most
people refer to it as "debugging" .

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 6 '07 #6
Randy Webb wrote:
Start removing other parts of the code until it works. Then start adding
them back until it breaks. That is how you find errors/problems and most
people refer to it as "debugging" .
Oh, no! Not "de bug ging." Dem come 'round and steal me good tots, me
code suffer large mon.

I hate it when "de bug ging" visit me 'ere. De bugging me!

*runs away screaming from de bugs*

De bugs scare de' caca clean out ah me!

Ya, mon.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 6 '07 #7
"Mark Anderson" <ma**@notmeyear dley.demon.co.u kwrote in message
news:f4******** ***********@new s.demon.co.uk.. .
----- Original Message -----
From: "Randy Webb" <Hi************ @aol.com>
Newsgroups: comp.lang.javas cript
Sent: Monday, June 04, 2007 10:24 PM
Subject: Re: window.onload fails from external JS file

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

<snip>
>>window.onlo ad = 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.onloa d = addMyEvent;

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.ge tElementById || !document.creat eTextNode){retu rn;}
// we'll only trap <aand <imginside ID 'imageLoc'
killDrag.n=docu ment.getElement ById('imageLoc' );
if(!killDrag.n) {return;}
killDrag.addMyL isteners('mouse down',killDrag. suppressDrag,'i mg');
killDrag.addMyL isteners('mouse down',killDrag. suppressDrag,'a ');
},
addMyListeners: function(eventN ame,functionNam e,elements){
var temp=killDrag.n .getElementsByT agName(elements );
for(var i=0;i<temp.leng th;i++){
temp[i].addEventListen er(eventName,fu nctionName,fals e);
}
},
suppressDrag:fu nction(e){
e.stopPropagati on();
e.preventDefaul t();
}
};
window.addEvent Listener('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
Figured it out. IE was choking on this line
window.addEvent Listener('load' ,killDrag.init, false);

Changing it to

if (window.addEven tListener) {
window.addEvent Listener('load' ,killDrag.init, false);
}

.....and using window.onload = addMyEvent; without the () and it works.

Thought I'd post it in case it helps anyne else.

Regards

Mark
Jun 6 '07 #8
Mark Anderson wrote:
Figured it out. IE was choking on this line
window.addEvent Listener('load' ,killDrag.init, false);

Changing it to

if (window.addEven tListener) {
window.addEvent Listener('load' ,killDrag.init, false);
}

....and using window.onload = addMyEvent; without the () and it works.

Thought I'd post it in case it helps anyne else.
You erroneously assume that everyone else does not initiate proper
feature detection routines and/or fails to realize that Internet
Explorer does not adhere to the Gecko DOM, or JavaScript standards.

It is wise to always check for specific features you require. In that
fashion you eliminate having to go through what you did.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 7 '07 #9
dd
On Jun 7, 2:36 am, -Lost <maventheextraw o...@techie.com wrote:
Thought I'd post it in case it helps anyne else.

You erroneously assume that everyone else does not initiate proper
feature detection routines and/or fails to realize that Internet
Explorer does not adhere to the Gecko DOM, or JavaScript standards.
I think the JavaScript error saying that IE doesn't understand
addeventlistene r would have been the biggest hint in this story. Of
course you can turn off those nasty (helpful) error messages.

Jun 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4559
by: Brian | last post by:
Hi everyone, I'm writing a function (in javascript) that needs to do one thing if the page has not loaded, and another (different) thing if the page has already loaded. I'm looking for a way to tell if the window.onload event has already fired. I cannot edit the onload event handler itself, and my function can only exist in an external...
8
7970
by: Mark | last post by:
There are no problems with the following image loading code fragment and window.status assigments/updates in IE but in Firefox window.status fails to update. Any suggestions? (function imageLoader() { for (i=0; i <= 358; i+=2) { imgSRC = 'cue'+i+'.gif'; cues = new Image(); cues.cnt = 0; cues.onload = imagesToLoad(i);
10
1917
by: Pasquale | last post by:
hello wverybody... i've got a terrible matter with JS my browsers (either IE and NN) load the external scripts uncorrectly...they load the files from the half part of them and not from the beginning, so my scripts don't work giving syntax errors. can you give me a solution?
3
10188
by: Liming | last post by:
Hi, Here is my situation. I have a textarea on the page. When the page loads, I need it to have some default text (which will be generated dynamically) so I did something like this function init() {
3
1786
by: Frances | last post by:
I have three functions I need triggered when page loads, so have <body onload="function1();function2();function3()"> but I want to take all these function calls out of body tag and call them in header, thus: window.onload=function1;function2; but I just realized that only first function in this list gets called
7
1946
by: Andrew Poulos | last post by:
In the head of my page I have: <script type="text/javascript" src="scripts/test.js"></script> and later in the head I have: <script type="text/javascript"> window.onload = function() { foo = 1; };
5
9425
by: lilOlMe | last post by:
Hi there! I'm developing some crazy Tab Control in .NET that uses JavaScript. A particular JavaScript method needs to be called during the window.onload event in order to initialize my Tab Control. The thing is that there can be more than one Tab Control on the page....and each one must be initialized during the window.onload event. ...
0
7832
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8129
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6498
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3768
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3785
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2262
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1094
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.