469,271 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

really difficult IE quirk getAttribute and setAttribute or .replace problem

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>
<titletesting </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('onco ntextmenu',
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

Apr 4 '07 #1
4 4861
ICPooreMan wrote:
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>
<titletesting </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('onco ntextmenu',
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
Apr 5 '07 #2
On Apr 5, 7:01 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
ICPooreMan wrote:
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>
<titletesting </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('onco ntextmenu',
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

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').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.expando = false;
document.getElementById('div1').setAttribute('onco ntextmenu',
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.

Apr 5 '07 #3
ICPooreMan wrote:
On Apr 5, 7:01 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>ICPooreMan wrote:
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>
<titletesting </title>
<script><!--
function doSomething(){
var someText =
String(document.getElementById('div1').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('onco ntextmenu',
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


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').getAttribut e('oncontextmenu'));
someText = someText.replace("Hello","World");
document.expando = false;
document.getElementById('div1').setAttribute('onco ntextmenu',
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
Apr 5 '07 #4
ICPooreMan wrote:
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('onco ntextmenu', someText);

and

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

are not the same for IE.
Apr 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ruslan | last post: by
5 posts views Thread by gkelly | last post: by
7 posts views Thread by tader | last post: by
4 posts views Thread by RobG | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.