Solved

IE7 getAttribute and setAttribute problem

Posted on 2007-04-04
5
2,752 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
Comment Utility
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
Comment Utility
nope didn't seem to change anything did you try the above???
0
 

Author Comment

by:ICPooreman
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now