Solved

IE7 getAttribute and setAttribute problem

Posted on 2007-04-04
5
2,778 Views
Last Modified: 2012-05-05
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
Comment
Question by:ICPooreman
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:stanscott2
ID: 18852942
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
 

Author Comment

by:ICPooreman
ID: 18853001
nope didn't seem to change anything did you try the above???
0
 

Author Comment

by:ICPooreman
ID: 18935011
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
 
LVL 15

Accepted Solution

by:
stanscott2 earned 500 total points
ID: 18935534
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
 

Author Comment

by:ICPooreman
ID: 18938814
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can I enable / disable a text box with a checkbox 3 23
Jquery 2 22
How to add a stringified JSON string to an IP address? 11 56
100% tall div not scrollable on iPhone 3 20
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question