Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2821
  • Last Modified:

IE7 getAttribute and setAttribute 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>
<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.
0
ICPooreman
Asked:
ICPooreman
  • 3
  • 2
1 Solution
 
stanscott2Commented:
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;
}
0
 
ICPooremanAuthor Commented:
nope didn't seem to change anything did you try the above???
0
 
ICPooremanAuthor Commented:
well I've looked into this a little more and still no real answer but some observations.  
1) the problem exists in both ie6 + ie7
2) it's not a prolem with setAttribute because setAttribute works when changed like so
function doSomething(){
var someText = String(document.getElementById('div1').getAttribute('oncontextmenu'));
someText = someText.replace("Hello","World");
document.getElementById('div1').setAttribute('oncontextmenu',
function(){ alert('World world');return false; } );
}

However there doesn't seem to be a way to change the value of a function and I can't find a way to make my string evaluate as if it was literally typed out.  Even though the string itself literally says the same thing it's evaluated as a string in IE and not a function.

I know this is an old thread and the answer probably is that it can't be done but any help or insight is greatly appreciated.
0
 
stanscott2Commented:
Maybe you haven't given the whole problem, but the solution to the example you've given is to store the text displayed in the alert outside the oncontextmenu string.  Once you do this, you can change it as often as necessary:

<html>
<head>
<title> testing </title>
<script type="text/javascript">
     var someText="Hello World";

     function doSomething(){
          someText = someText.replace("Hello","World");
     }
</script>
</head>
<body>
<div id="div1" onclick="doSomething();return false;" oncontextmenu="alert(someText);return false;">Hello</div>
</body>
</html>
0
 
ICPooremanAuthor Commented:
Ya I think I've concluded that exactly what I want to do can't be done and I'll just have to change strings to variables which can be changed dynamically.  Thanks.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now