CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

Extend an event in javascript

If you use jQuery, you don’t need it. But if you for some reason must use javascript you must beware of adding events. The most dangerous is window.onload. Consider this scenario:

function doSomethingGreat() {};
window.onload = doSomethingGreat;

It works fine if you are the only person who writes window.onload handler. But on such platforms like SharePoint you can’t know which events exist. And if you just write window.onload = doSomethingGreat; you override all other window.onload events.

Again, in jQuery you have nothing to worry. jQuery(window).load(doSomethingGreat) will just add your event, not override. In this post I’ll show how to extend an event handler the pure javascript way. We have to create our own function for adding event handlers. First the old events have to be temporary saved and then the new event has to be added. Like this:

function addOnclick(elem, func) {
    var old = elem.onclick;
    if (typeof elem.onclick != 'function') {
        elem.onclick = func;
    } else {
        elem.onclick = function () {
            if (old) {
                old();
            }
            func();
        };        
    }
}

func1 = function() {
    window.console.log("lapin");
}
func2 = function() {
    window.console.log("kulta");
}

var elem = document.getElementById("mydiv");
addOnclick(elem, func1);
addOnclick(elem, func2);

Unfortunately += operator doesn’t work. If you do so, the onclick event just get a string of two functions, and the click event doesn’t do anything:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Discovering SharePoint

And having fun doing it

Bram de Jager talking Office 365, SharePoint and Azure

My view and thoughts on Productivity and more.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

RealActivity - Real-time and trustworthy

Blog site of founder, RealActivty - Paul J. Swider

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points!!

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode, ya mama

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

blksthl

Mostly what I know about SharePoint - CommunicoCuspis

%d bloggers like this: