Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2012, 04:03 PM   PM User | #1
cbanks
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cbanks is an unknown quantity at this point
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..
cbanks is offline   Reply With Quote
Old 11-07-2012, 04:25 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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..
Philip M is offline   Reply With Quote
Old 11-07-2012, 04:34 PM   PM User | #3
cbanks
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cbanks is an unknown quantity at this point
I changed the line below, but it still doesn't work. What am I missing?

Code:
document.attachEvent('onclick', function(event){
cbanks is offline   Reply With Quote
Old 11-07-2012, 06:36 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
You might need to show a bit more of the code. How have you combined addEventListener and attachEvent together?
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-07-2012, 07:01 PM   PM User | #5
cbanks
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cbanks is an unknown quantity at this point
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()">
cbanks is offline   Reply With Quote
Old 11-07-2012, 07:28 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-07-2012, 08:46 PM   PM User | #7
cbanks
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cbanks is an unknown quantity at this point
I took a look at that page and I'm still not sure what changes need to be made.
cbanks is offline   Reply With Quote
Old 11-07-2012, 09:10 PM   PM User | #8
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-07-2012, 10:25 PM   PM User | #9
cbanks
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cbanks is an unknown quantity at this point
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.
cbanks is offline   Reply With Quote
Old 11-08-2012, 12:04 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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..
Old Pedant is offline   Reply With Quote
Old 11-08-2012, 03:24 PM   PM User | #11
cbanks
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cbanks is an unknown quantity at this point
I can't get either one of those code examples to work.
cbanks is offline   Reply With Quote
Old 11-08-2012, 06:52 PM   PM User | #12
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by cbanks View Post
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).
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-08-2012, 07:32 PM   PM User | #13
cbanks
New to the CF scene

 
Join Date: Nov 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
cbanks is an unknown quantity at this point
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?
cbanks is offline   Reply With Quote
Old 11-08-2012, 07:45 PM   PM User | #14
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-08-2012, 08:01 PM   PM User | #15
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:21 AM.


Advertisement
Log in to turn off these ads.