IE changing an onclick event dynamically

I

ICPooreMan

The following is a very simple example of what I want to do take an
elements oncontextmenu and changing it dynamically onclick of that
same element. The code below will fail unless you change the line

document.getElementById('div1').setAttribute('oncontextmenu',
someText);
to
document.getElementById('div1').setAttribute('oncontextmenu',
function(){alert('World World');return false;});

<html>
<head>
<title> testing </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribute('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('oncontextmenu',
someText);
}
//-->
</script>
</head>
<body>
<div id="div1" onclick="doSomething();return false;"
oncontextmenu="alert('Hello World');return false;"> Hello </div>
</body>
</html>

The string I generate in the example is what the function should say
so is there anyway to take that string and make a literal translation
in IE?

I posted a while back something similar because I thought there was a
problem with the setAttribute function, no real great answers were
found but after further inspection it turns out setAttribute works it
just won't translate a string to a function. PS only IE has problems
with this I've tested with 6+7
 
D

Daz

The following is a very simple example of what I want to do take an
elements oncontextmenu and changing it dynamically onclick of that
same element. The code below will fail unless you change the line

document.getElementById('div1').setAttribute('oncontextmenu',
someText);
to
document.getElementById('div1').setAttribute('oncontextmenu',
function(){alert('World World');return false;});

<html>
<head>
<title> testing </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribute('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('oncontextmenu',
someText);}

//-->
</script>
</head>
<body>
<div id="div1" onclick="doSomething();return false;"
oncontextmenu="alert('Hello World');return false;"> Hello </div>
</body>
</html>

The string I generate in the example is what the function should say
so is there anyway to take that string and make a literal translation
in IE?

I posted a while back something similar because I thought there was a
problem with the setAttribute function, no real great answers were
found but after further inspection it turns out setAttribute works it
just won't translate a string to a function. PS only IE has problems
with this I've tested with 6+7

Perhaps I am missing something here (as it's 3:32AM and I'm tired),
but oncontextmenu is not an attribute, it's an event. Shouldn't you be
adding it using the addEventHandler() method? Also, someText is a
string, and I believe it should be a function. Sorry for sounding
ignorant, but what are you actually trying to do? I am sure I have it
wrong.
 
I

ICPooreMan

Perhaps I am missing something here (as it's 3:32AM and I'm tired),
but oncontextmenu is not an attribute, it's an event. Shouldn't you be
adding it using the addEventHandler() method? Also, someText is a
string, and I believe it should be a function. Sorry for sounding
ignorant, but what are you actually trying to do? I am sure I have it
wrong.


I've tried addEventHandler and it doesn't seem to work for the same
reason as above. I think you are missing what I'm asking what I'm
looking to do is dynamically change some variables in the
oncontextmenu event, in order to do that the way it's set up now I
need to convert it to a string so that I can replace out things I want
to change. However, once I convert it to a string it's no longer a
function and is useless in that context. But there's no way as far as
I can tell to turn it back to a function from a string and that's the
problem it seems what I want to do can't be done.
 
D

Daz

I've tried addEventHandler and it doesn't seem to work for the same
reason as above. I think you are missing what I'm asking what I'm
looking to do is dynamically change some variables in the
oncontextmenu event, in order to do that the way it's set up now I
need to convert it to a string so that I can replace out things I want
to change. However, once I convert it to a string it's no longer a
function and is useless in that context. But there's no way as far as
I can tell to turn it back to a function from a string and that's the
problem it seems what I want to do can't be done.

I'm still not entirely sure what you mean. I am sure you've already
seen this, but if not, please take a look at it:
http://msdn.microsoft.com/workshop/author/dhtml/reference/events/oncontextmenu.asp

It looks like you can do what you want, but not my using standard
JavaScript. I would suggest you look into adding a hard coded event
handler, and making the function do all the dynamic stuff. I trust you
are aware that your web app won't work on any browser other than IE?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top