Enjoy an ad free experience by logging in. Not a member yet?
Register .
11-07-2012, 04:03 PM
PM User |
#1
New to the CF scene
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
How to change Javascript to work with IE
Code:
<style type="text/css">
.highlight{
background-color: orange;
color: white;
}
</style>
<script type="text/javascript">
function createListener(){
document.addEventListener('click', function(event){
var element=event.target;
if(element!==document.body){
if(element.className===''){
element.className='highlight';
} else {
element.className='';
}
}
}, false);
}
</script>
<body onload="createListener()">
Last edited by cbanks; 11-07-2012 at 04:27 PM ..
11-07-2012, 04:25 PM
PM User |
#2
Supreme Master coder!
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
The standard way to attach event listeners is to use addEventListener but IE8 and earlier use attachEvent instead.
http://javascript.about.com/library/bldom19.htm
http://www.howtocreate.co.uk/tutoria...ript/domevents
BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Last edited by Philip M; 11-07-2012 at 04:27 PM ..
11-07-2012, 04:34 PM
PM User |
#3
New to the CF scene
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I changed the line below, but it still doesn't work. What am I missing?
Code:
document.attachEvent('onclick', function(event){
11-07-2012, 06:36 PM
PM User |
#4
Master Coder
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
You might need to show a bit more of the code. How have you combined addEventListener and attachEvent together?
11-07-2012, 07:01 PM
PM User |
#5
New to the CF scene
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I changed the code to what appears below, but it still doesn't work.
Code:
<style type="text/css">
.highlight{
background-color: orange;
color: white;
}
</style>
<script type="text/javascript">
function createListener(){
document.attachEvent('onclick', function(event){
var element=event.target;
if(element!==document.body){
if(element.className===''){
element.className='highlight';
} else {
element.className='';
}
}
}, false);
}
</script>
<body onload="createListener()">
11-07-2012, 07:28 PM
PM User |
#6
Supreme Master coder!
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Try using the forum search feaure. You will find very extensive information by felgall at
http://www.codingforums.com/showthread.php?t=265184
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
11-07-2012, 08:46 PM
PM User |
#7
New to the CF scene
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I took a look at that page and I'm still not sure what changes need to be made.
11-07-2012, 09:10 PM
PM User |
#8
Master Coder
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
You need to use whichever of the two types of call that the browser being used supports. The latest example of the code you posted specifically uses attachEvent and so will only work in Internet Explorer.
Substitute a call to a new addEvent function in place of attachEvent and then add code that creates that function to call either addEventListener or attachEvent depending on which the browser supports.
11-07-2012, 10:25 PM
PM User |
#9
New to the CF scene
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I don't need it to work for other browsers, just IE 8 and older, so that's why I changed some of the code to use attachEvent instead. I just don't know what else to add or change to make the code work.
11-08-2012, 12:04 AM
PM User |
#10
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
That code is wrong.
MSIE doesn't pass the
event object to the event handlers.
You need to simply get it via
window.event within the body of the function.
Also, attachEvent only takes two arguments. Though I can't see how passing the third one would hurt.
Also, the window.event object does *NOT* have any property named
target . The correct property is
srcElement .
If you are going to do MSIE-only development, maybe you should read the MSIE docs?
Try this:
Code:
<html>
<head>
<style type="text/css">
.highlight{ background-color: orange; color: white; }
</style>
</head>
<body>
stuff and more
<input type="button" value="click me" />
<script type="text/javascript">
function createListener()
{
document.attachEvent('onclick',
function()
{
var evt = window.event;
var element = evt.srcElement;
if(element!==document.body)
{
if(element.className=="") { element.className='highlight'; }
else { element.className=''; }
}
}
);
}
createListener();
</script>
</body>
</html>
Or, even simpler:
Code:
<html>
<head>
<style type="text/css">
.highlight{ background-color: orange; color: white; }
</style>
</head>
<body>
stuff and more
<input type="button" value="click me" />
<script type="text/javascript">
document.attachEvent('onclick',
function()
{
var elem = window.event.srcElement;
if( elem !== document.body )
{
elem.className = (elem.className=="") ? "highlight" : "";
}
}
);
</script>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 11-08-2012 at 12:12 AM ..
11-08-2012, 03:24 PM
PM User |
#11
New to the CF scene
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I can't get either one of those code examples to work.
11-08-2012, 06:52 PM
PM User |
#12
Master Coder
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
Quote:
Originally Posted by
cbanks
I can't get either one of those code examples to work.
Which version of IE are you using? That code should work fine in IE6, 7 and 8 (and probably 5 as well).
11-08-2012, 07:32 PM
PM User |
#13
New to the CF scene
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
I got it to work. But when I save the page I want it to save the highlighting changes that I've made. How would I do that?
11-08-2012, 07:45 PM
PM User |
#14
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
You can't "save" an HTML page.
What is "save" supposed to mean?
You mean bookmark it?
Regardless of whether it is bookmarked or not, you probably need to learn to use cookies to maintain page state .
Note that cookies are on a PER MACHINE PER USER basis. So highlighting done by Joe won't be seen by Mary.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
11-08-2012, 08:01 PM
PM User |
#15
Master Coder
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
The only way to"save" the changes so everyone can see them is to use a call to the server to pass the changes so that a server side script can update the original page source.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 04:21 AM .
Advertisement
Log in to turn off these ads.