really difficult IE quirk getAttribute and setAttribute or .replace problem

I

ICPooreMan

I've got some code which works in firefox that's giving me fits in
IE7 (maybe other versions too I haven't tested it). What I want to do
is get the oncontextmenu attribute of something, change the value then
put it back as the oncontextmenu attribute.
Here's an example page if you want to try it out...

<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 problem is after the onclick event oncontextmenu no longer works.
I've figured out that the reason is that I cast the value of
oncontextmenu to a string. Without that cast setAttribute will work
correctly. However without the cast the replace method will not work.
So I either need a way to cast the variable back or do a replace
without the cast.

ps this works fine in firefox
 
E

Erwin Moller

ICPooreMan said:
I've got some code which works in firefox that's giving me fits in
IE7 (maybe other versions too I haven't tested it). What I want to do
is get the oncontextmenu attribute of something, change the value then
put it back as the oncontextmenu attribute.
Here's an example page if you want to try it out...

<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 problem is after the onclick event oncontextmenu no longer works.
I've figured out that the reason is that I cast the value of
oncontextmenu to a string. Without that cast setAttribute will work
correctly. However without the cast the replace method will not work.
So I either need a way to cast the variable back or do a replace
without the cast.

ps this works fine in firefox

Hi,

Why make things so complicated?
I would simply make the two eventhandlers call two functions, and keep track
of things you need in there (like changing the content of the alert).

I think if you want it to work the way you did now, you should assign a new
function to the oncontextmenu event handler, and not replace text in an
existing one. (Not sure).

Regards,
Erwin Moller
 
I

ICPooreMan

Hi,

Why make things so complicated?
I would simply make the two eventhandlers call two functions, and keep track
of things you need in there (like changing the content of the alert).

I think if you want it to work the way you did now, you should assign a new
function to the oncontextmenu event handler, and not replace text in an
existing one. (Not sure).

Regards,
Erwin Moller


Thanks, well In reality I do have 2 functions I was just giving a
simple example of the bug. In my actual application there are a lot
of different divs that will send an oncontextmenu event all calling
the same function with a couple of variables the variables which may
be changed from time to time. I could keep track of them globally I
suppose but that could end up getting quite messy I like this as it's
a much cleaner solution for what I'm doing. Although if I can't
figure out a way around this I may be forced to do something of the
sort...

I've also asked this in other forums and nobody has responded the only
response I've received was.

"There is an issue with the way IE7 sets attributes. Try this in your
function:

function doSomething(){
var someText =
String(document.getElementById('div1').getAttribute('oncontextmenu'));
someText = someText.replace("Hello","World");
document.expando = false;
document.getElementById('div1').setAttribute('oncontextmenu',
someText);
document.expando = true;
}
"

which even though there is a bug in setAttribute and that is the fix
for it still didn't fix my problem.
 
E

Erwin Moller

ICPooreMan said:
Thanks, well In reality I do have 2 functions I was just giving a
simple example of the bug. In my actual application there are a lot
of different divs that will send an oncontextmenu event all calling
the same function with a couple of variables the variables which may
be changed from time to time. I could keep track of them globally I
suppose but that could end up getting quite messy I like this as it's
a much cleaner solution for what I'm doing. Although if I can't
figure out a way around this I may be forced to do something of the
sort...

I've also asked this in other forums and nobody has responded the only
response I've received was.

"There is an issue with the way IE7 sets attributes. Try this in your
function:

function doSomething(){
var someText =
String(document.getElementById('div1').getAttribute('oncontextmenu'));
someText = someText.replace("Hello","World");
document.expando = false;
document.getElementById('div1').setAttribute('oncontextmenu',
someText);
document.expando = true;
}
"

which even though there is a bug in setAttribute and that is the fix
for it still didn't fix my problem.

Hi,

Well, I never used that expando thingy, so I cannot help you there.
Personally I avoid tricky solutions like hell and keep things simple with
basic Javascript and basic approach to eventhandlers.

I do not see why having a global object tends to be messy, but of course I
don't know your situation.

This is how I do stuff like this:

If you have, say, 50 divs on your page, and you need to store information
because the interaction is complex, just store it in an Object:

[just a silly example]
var myDivStates = new Object();
myDivStates["div1"] = new Object();
myDivStates["div1"]["alerttext"] = "world";
myDivStates["div1"]["opened"] = true;
myDivStates["div1"]["clicked"] = false;

and the same for
myDivStates["div2"]
or whatever names suits you.

And use them later to check if they contain 'world' or 'hello'.

I understand your app is more complicated, but storing stuff in arrays or
objects shouldn't have to be messy if you think up a nice structure first.

Of course, you ARE storing information in JS that is in most cases already
in the HTML-document, but who cares about a few K extra memory if it keeps
your JS clean and working?

just my 2 cents.

Regards,
Erwin Moller
 
W

Wenzel.Peppmeyer

ICPooreMan said:
The problem is after the onclick event oncontextmenu no longer works.
I've figured out that the reason is that I cast the value of
oncontextmenu to a string. Without that cast setAttribute will work
correctly. However without the cast the replace method will not work.
So I either need a way to cast the variable back or do a replace
without the cast.

Sadly I cant tell you how to solve it but your problem is basicly that

document.getElementById('div1').setAttribute('oncontextmenu', someText);

and

document.getElementById('div1').oncontextment = someText;

are not the same for 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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top