473,570 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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').getAtt ribute('onconte xtmenu'));
someText = someText.replac e("Hello","Worl d");
document.getEle mentById('div1' ).setAttribute( 'oncontextmenu' ,
someText);
}
//-->
</script>
</head>
<body>
<div id="div1" onclick="doSome thing();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 5041
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').getAtt ribute('onconte xtmenu'));
someText = someText.replac e("Hello","Worl d");
document.getEle mentById('div1' ).setAttribute( 'oncontextmenu' ,
someText);
}
//-->
</script>
</head>
<body>
<div id="div1" onclick="doSome thing();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_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
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').getAtt ribute('onconte xtmenu'));
someText = someText.replac e("Hello","Worl d");
document.getEle mentById('div1' ).setAttribute( 'oncontextmenu' ,
someText);
}
//-->
</script>
</head>
<body>
<div id="div1" onclick="doSome thing();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').getAtt ribute('onconte xtmenu'));
someText = someText.replac e("Hello","Worl d");
document.expand o = false;
document.getEle mentById('div1' ).setAttribute( 'oncontextmenu' ,
someText);
document.expand o = 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_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
>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').getAtt ribute('onconte xtmenu'));
someText = someText.replac e("Hello","Worl d");
document.getEle mentById('div1' ).setAttribute( 'oncontextmenu' ,
someText);
}
//-->
</script>
</head>
<body>
<div id="div1" onclick="doSome thing();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').getAtt ribute('onconte xtmenu'));
someText = someText.replac e("Hello","Worl d");
document.expand o = false;
document.getEle mentById('div1' ).setAttribute( 'oncontextmenu' ,
someText);
document.expand o = 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.getEle mentById('div1' ).setAttribute( 'oncontextmenu' , someText);

and

document.getEle mentById('div1' ).oncontextment = someText;

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4887
by: Ruslan | last post by:
Hi, everybody. In this excerpt of code enc = 'some_type_of_encoding' def _encode(v): if isinstance(v, UnicodeType): v = v.encode(v) return v
2
5454
by: CES | last post by:
All, I'm at a loss, the code below works in IE but not in Netscape and I'm unskilled enough not to know why. Essentially this code looks thru all of the form fields and if the input box has a attribute of disabled="true", attribute would only be present if the field was disabled, then the label field's class is changed to a grayed out...
3
3464
by: duff | last post by:
Hiya, I'm trying to replace the commented out code below with a neater version using the IAttributeAccessor interface (so that I don't have to write 'if' statements for all control types). At the moment this doen't work. The test line I put in (string s = ControlAttributes.string s = ControlAttributes.GetAttribute ("Text");
5
2244
by: gkelly | last post by:
Can someone explain what I am doing wrong, or why this will not work? I've tested this in IE6, Firefox 1.5 and Mozilla 1.7, all with the same result. Take for example this code: <html> <head> <script> function replace(){
8
1651
by: pbd22 | last post by:
hi. i have spent the past week (i am afraid) trying to get the below script for uploading files via a hidden iframe to work. i have narrowed down my problem to the possibility that IE doesnt like the fact that the script is instantiating the object type so many times (of course, tell me if i am wrong).
7
2216
by: tader | last post by:
Hi, so i done script like that body : function() { for (i = 0; i < all_divs.length; i++) { if (all_divs.title) { addEvent(all_divs, "mouseover", this.onover); addEvent(all_divs, "mouseout", this.onout); all_divs.setAttribute("tooltip", all_divs.title);
4
2874
by: RobG | last post by:
I have always accessed attributes such as disabled using the DOM element property, however I was wondering about implementing a more generic function to get the values of attributes - which of course leads to the DOM Element Interface's getAttribute method: <URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-666EE0F9 > The DOM 2 Core...
11
3331
by: maminx | last post by:
Hello all,, i have a question, please help me to find out i have this script (in js function) var td = document.createElement('td'); var p = document.createElement('p'); var label = document.createElement('label'); var span = document.createElement('span'); var theTextarea = document.createElement('textarea');...
3
2547
by: maminx | last post by:
I have this script... var td = document.createElement('td'); var p = document.createElement('p'); var label = document.createElement('label'); var span = document.createElement('span'); var theData = document.createTextNode('Chart Of Account'); var theSelect = document.createElement('select'); theSelect.setAttribute('name',...
0
8150
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7700
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6329
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5247
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3671
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.