Skip to content

Instantly share code, notes, and snippets.

@HendrikEduard
Created September 19, 2018 08:18
Show Gist options
  • Save HendrikEduard/a97a4e4c13f7830b9a6ca19364be4354 to your computer and use it in GitHub Desktop.
Save HendrikEduard/a97a4e4c13f7830b9a6ca19364be4354 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Eze_JS event listeners</title>
<script>
function addListeners() {
if(window.addEventListener) {
document.getElementById('button1').addEventListener("click", button1function, false);
document.getElementById('button2').addEventListener("mouseover", button2function, false);
// For Internet Explorer versions IE-8 and older
} else if (window.attachEvent) {
document.getElementById('button1').attachEvent("onclick", button1function);
document.getElementById('button2').attachEvent("onmouseover", button2function);
}
// functions
function button1function() {
alert(this.id+" : mouse-click runs the script");
}
function button2function() {
alert(this.id+" : mouse-over runs the script");
}
}
window.onload = addListeners;
</script>
</head>
<body>
<section class="main">
<button id="button1">Click Event</button>
<button id="button2">Hover Event</button>
</section>
</body>
</html>
Copyright (c) 2018 by H E Kuiper
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Eze_JS modern event listeners</title>
<!-- ONLY supports IE-9 and NEWER -->
<script>
function addListeners() {
document.getElementById('button1').addEventListener("click", button1function, false);
document.getElementById('button2').addEventListener("mouseover", button2function, false);
function button1function() {
alert(this.id+" : mouse-click runs the script.");
}
function button2function() {
alert(this.id+" : mouse-over runs the script.");
}
}
window.addEventListener("load", addListeners);
</script>
</head>
<body>
<section class="main">
<button id="button1">Click Event</button>
<button id="button2">Hover Event</button>
</section>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment