473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Removing in line javascript events

I read about this technique to basically allow you to have a file with
behaviours and javascript events that attach to various dom objects
and events but are not done inline.

so its basically a css file but for javascript.

anyone know what it is called or where i can get more information
about it??

thanks

Jun 11 '07 #1
4 1876
On Jun 11, 3:53 pm, m0nkeymafia <m0nkeyma...@hotmail.co.ukwrote:
I read about this technique to basically allow you to have a file with
behaviours and javascript events that attach to various dom objects
and events but are not done inline.

so its basically a css file but for javascript.

anyone know what it is called or where i can get more information
about it??

thanks
No, the technique looks like the following:
<html>
<script href="events.js"></script>
<body>
<div id="housing">
<!------------------------------------------->
</div>

<div id="humanitarian">
<!------------------------------------------->
</div>
</body>
</html>

// events.js:
document.onload = function()
{
document.getElementById( "housing" ).onclick = function()
{
// ...
}

document.getElementById( "humanitarian" ).onmousemove =
function()
{
// ...
}
}

Does this serve your needs?

Jun 11 '07 #2
On Jun 11, 3:53 pm, m0nkeymafia <m0nkeyma...@hotmail.co.ukwrote:
I read about this technique to basically allow you to have a file with
behaviours and javascript events that attach to various dom objects
and events but are not done inline.

so its basically a css file but for javascript.

anyone know what it is called or where i can get more information
about it??

thanks
No, the technique looks like the following:
<html>
<script href="events.js"></script>
<body>
<div id="housing">
<!------------------------------------------->
</div>

<div id="humanitarian">
<!------------------------------------------->
</div>
</body>
</html>

// events.js:
document.onload = function()
{
document.getElementById( "housing" ).onclick = function()
{
// ...
}

document.getElementById( "humanitarian" ).onmousemove = function()
{
// ...
}
}

Jun 11 '07 #3
On Jun 11, 4:24 pm, Darko <darko.maksimo...@gmail.comwrote:
On Jun 11, 3:53 pm, m0nkeymafia <m0nkeyma...@hotmail.co.ukwrote:
I read about this technique to basically allow you to have a file with
behaviours and javascript events that attach to various dom objects
and events but are not done inline.
so its basically a css file but for javascript.
anyone know what it is called or where i can get more information
about it??
thanks

No, the technique looks like the following:
<html>
<script href="events.js"></script>
<body>
<div id="housing">
<!------------------------------------------->
</div>

<div id="humanitarian">
<!------------------------------------------->
</div>
</body>
</html>

// events.js:
document.onload = function()
{
document.getElementById( "housing" ).onclick = function()
{
// ...
}

document.getElementById( "humanitarian" ).onmousemove = function()
{
// ...
}

}
Ah brilliant thanks, is that safe to do [closures etc] ?
Or is it generally a bad technique?

Jun 11 '07 #4
On Jun 11, 4:29 pm, m0nkeymafia <m0nkeyma...@hotmail.co.ukwrote:
On Jun 11, 4:24 pm, Darko <darko.maksimo...@gmail.comwrote:
On Jun 11, 3:53 pm, m0nkeymafia <m0nkeyma...@hotmail.co.ukwrote:
I read about this technique to basically allow you to have a file with
behaviours and javascript events that attach to various dom objects
and events but are not done inline.
so its basically a css file but for javascript.
anyone know what it is called or where i can get more information
about it??
thanks
No, the technique looks like the following:
<html>
<script href="events.js"></script>
<body>
<div id="housing">
<!------------------------------------------->
</div>
<div id="humanitarian">
<!------------------------------------------->
</div>
</body>
</html>
// events.js:
document.onload = function()
{
document.getElementById( "housing" ).onclick = function()
{
// ...
}
document.getElementById( "humanitarian" ).onmousemove = function()
{
// ...
}
}

Ah brilliant thanks, is that safe to do [closures etc] ?
Or is it generally a bad technique?
Well, it is safe per se, but it is true that programmers are prone to
making mistakes in such contexts, such as not using "this" as they
should. For example, look at the following example, and think what's
the problem with it:

<html>
<script src="events.js"></script>
<body>
<div id="housing">
housing
</div>

<div id="humanitarian">
humanitarian
</div>
</body>
</html>

// events.js:

// instead of creating event handlers on the fly, i.e. in the big
document.onload function, we shall e.g. create an Event Handlers
Factory:
function EventHandlersFactory()
{
// the factory keeps track of how many event handlers it created
so far
this.nrEventHandlers = 0;
}

// the factory has only one method:
EventHandlersFactory.prototype.getEventHandler = function()
{
// it updates the attribute
this.nrEventHandlers++;
// and returns the function that will execute at the specified
moment. It should only alert the ordinal number of the event.
return function()
{
alert( "I am the " + this.nrEventHandlers + ". event handler
created" );
}
}

window.onload = function()
{
alert( "nesto" );
var ehf = new EventHandlersFactory();
document.getElementById( "housing" ).onclick =
ehf.getEventHandler();
document.getElementById( "humanitarian" ).onclick =
ehf.getEventHandler();
}

// question: what happens when you click on the "humanitarian"-box?

// Please note the two minor corrections inhere: it's not
document.onload but window.onload, and it's not <script href=""but
<script src=""... lapsus calami :)

Jun 11 '07 #5

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

Similar topics

16
by: graham.reeds | last post by:
I am updating a website that uses a countdown script embedded on the page. When the page is served the var's are set to how long the countdown has left in minutes and seconds, but the rest of the...
3
by: Guadala Harry | last post by:
In the following line of code, what is the point of including the 'new' keyword? List.Changed -= new ChangedEventHandler(ListChanged); I'm just a bit confused because I thought 'new' was used...
2
by: Bob Rundle | last post by:
I have the following code, which appears to be working. However it doesn't look right. The part I am wondering about is the logic in DisconnectEvents(). This logic creates a new delegate and...
18
by: mouseit101 | last post by:
I'm writing a program to emulate nuerons (can't spell for my life sorry) and am using an array of "mailboxes" to propagate messages through the network, but I need to be able to remove the request...
1
by: huzheng001 | last post by:
I have develop a on-line dictionary website, http://www.stardict.org I meet a problem: Here is two lines of js codes: document.getElementById("wordlist").innerHTML = "";...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
2
by: beatTheDevil | last post by:
Hey guys, As the title says I'm trying to make a regular expression (regex/regexp) for use in removing the comments from code. In this case, this particular regex is meant to match /* ... */...
3
by: MikeP | last post by:
Hi there, I've tried hunting for this in the C# specs but can't find the relevant info. Does anyone know what the official rule says (or doesn't say) about removing an event more than once. If...
6
by: bgold12 | last post by:
Hey, I just want to make sure that when I remove an element I don't have to worry about the events listeners I added previously to the element. For example: // get the element by its id elem =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.