Javascript IE problem "unknown runtime error" when using innerHTML

J

John

Hi Everyone,

I'm having this extremely annoying problem with Internet Explorer 6,
giving me an error message saying "unknown runtime error" whenever I
try to alter the contents of a <div> element using innerHTML.

Now, I've researched this problem on the web, and found many references
to it, but none of them quite addressed my specific situation, and
since my experience with JavaScript is limited, I was not able to adapt
the solutions I found to my specific situation.

Anyway, it's all very basic:

I have a <div id="myDiv"></div> element that's inside a <form></form>
element. I'm trying to change the innerHTML of the <div> element when
clicking on a "radio" button.

This code works fine in Firefox:

<html>
<head>
<title>My Page</title>

<script type="text/javascript">
function ChangeContent(str)
{
var obj=document.getElementById("myDiv");

if (str=="display1")
{
obj.innerHTML = "<b>display 1 was selected</b>";
}
else if (str=="display2")
{
obj.innerHTML = "<b>display 2 was selected</b>";
}
}
</script>

</head>

<body>


<form>
Display1 <input type="radio" name="States" value="display1"
onchange="ChangeContent(this.value)" />
<br />
Display2 <input type="radio" name="States" value="display2"
onchange="ChangeContent(this.value)" />

<div id="myDiv"></div>
</form>


</body>

</html>

As I said, it works perfectly in Firefox, but in IE6 I get the error:
"unknown runtime error"
From what I could gather on the web, this is probably some kind of a
"parent" "child" problem, since the <div> element is inside of a form..
however since as I've mentioned my experience with JavaScript is
limited, I need a practical example of how I can solve this issue for
my particular situation...

Any help will be greatly appreciated!!!!!
 
A

ASM

John a écrit :

I haven't IE, but you could try :
<html>
<head>
<title>My Page</title>

<script type="text/javascript">
function ChangeContent(str)
{
var obj=document.getElementById("myDiv");

if(str.selected)
obj.innerHTML = (str.value=="display1")?
"<b>display 1 was selected<\/b>" :
(str.value=="display2")?
}
</script>

</head>

<body>

<form>
Display1 <input type="radio" name="States" value="display1"
onclick="ChangeContent(this)" />
<br />
Display2 <input type="radio" name="States" value="display2"
onclick="ChangeContent(this)" />

<div id="myDiv"></div>
</body>

</html>

As I said, it works perfectly in Firefox, but in IE6 I get the error:
"unknown runtime error"

Probably because you didn't escape / of said:
"parent" "child" problem, since the <div> element is inside of a form.

I hope it is not that.

On my idea it is curious to use onchange about a radio-button or a
checkbox where no content can change ...
 
S

Shinya Koizumi

with my ie i don't get any error message.
i am using ie6, too. i haven't apply the sp yet.


sk
 
D

dd

I've been programming JS and primarily testing it first on IE6 for
years
and have never seen an "unknown runtime error". Have you tried it on
other machines?

I have a suggestion for you though. You're not checking whether
the obj exists after your getElementById. It's always good practice
to do this construct:

var obj=document.getElementById(id);
if(obj){
//do stuff with obj
}

During testing you could have it alert that the object doesn't exist.

else alert("obj didn't exist with id=="+id);

Perhaps that's part of your problem, perhaps not. You'd normally
see an error that it doesn't exist, rather than an unknown runtime
error. Considering I've never seen such a thing though, I thought
I'd bring this to your attention. You never know.
 
Z

Zeraxp

Hi Everyone,

I'm having this extremely annoying problem with Internet Explorer 6,
giving me an error message saying "unknown runtime error" whenever I
try to alter the contents of a <div> element using innerHTML.

Now, I've researched this problem on the web, and found many references
to it, but none of them quite addressed my specific situation, and
since my experience with JavaScript is limited, I was not able to adapt
the solutions I found to my specific situation.

Anyway, it's all very basic:

I have a <div id="myDiv"></div> element that's inside a <form></form>
element. I'm trying to change the innerHTML of the <div> element when
clicking on a "radio" button.

This code works fine in Firefox:

<html>
<head>
<title>My Page</title>

<script type="text/javascript">
function ChangeContent(str)
{
var obj=document.getElementById("myDiv");

if (str=="display1")
{
obj.innerHTML = "<b>display 1 was selected</b>";
}
else if (str=="display2")
{
obj.innerHTML = "<b>display 2 was selected</b>";
}}

</script>

</head>

<body>

<form>
Display1 <input type="radio" name="States" value="display1"
onchange="ChangeContent(this.value)" />
<br />
Display2 <input type="radio" name="States" value="display2"
onchange="ChangeContent(this.value)" />

<div id="myDiv"></div>
</form>

</body>

</html>

As I said, it works perfectly in Firefox, but in IE6 I get the error:
"unknown runtime error"


"parent" "child" problem, since the <div> element is inside of a form..
however since as I've mentioned my experience with JavaScript is
limited, I need a practical example of how I can solve this issue for
my particular situation...

Any help will be greatly appreciated!!!!!


you had to try with a simple javascript string like "ok" or "not ok"
and your code should work but with html tags just need to escape / in
javascript strings !!!

your code modified :

<script type="text/javascript">
function ChangeContent(str)
{
var obj=document.getElementById("myDiv");

if (str=="display1")
{
obj.innerHTML = "<b>display 1 was selected<\/b>";
}
else if (str=="display2")
{
obj.innerHTML = "<b>display 2 was selected<\/b>";
}
}

</script>
 
M

matt.mmorgan

This code works fine in Firefox:
<script type="text/javascript">
function ChangeContent(str)
{
var obj=document.getElementById("myDiv");

if (str=="display1")
{
obj.innerHTML = "<b>display 1 was selected</b>";
}
else if (str=="display2")
{
obj.innerHTML = "<b>display 2 was selected</b>";
}}

</script>

The reason this code works in Firefox and not IE is due to your
calling of document.getElementByID("myDiv"); This method is the open
standards way of pulling objects in the DOM, but IE does not follow
this rule. Adding this check should allow it to work for both IE and
Firefox:

if(document.getElementByID) { //Open standards method
var obj=document.getElementById("myDiv");
}
else if(document.all) { //IE method
var obj=document.all['myDiv']; //Note the brackets rather than
parenthesis
}
 
V

VK

Hi Everyone,

I'm having this extremely annoying problem with Internet Explorer 6,
giving me an error message saying "unknown runtime error" whenever I
try to alter the contents of a <div> element using innerHTML.

Now, I've researched this problem on the web, and found many references
to it, but none of them quite addressed my specific situation, and
since my experience with JavaScript is limited, I was not able to adapt
the solutions I found to my specific situation.

Anyway, it's all very basic:

I have a <div id="myDiv"></div> element that's inside a <form></form>
element. I'm trying to change the innerHTML of the <div> element when
clicking on a "radio" button.

This code works fine in Firefox:

<html>
<head>
<title>My Page</title>

<script type="text/javascript">
function ChangeContent(str)
{
var obj=document.getElementById("myDiv");

if (str=="display1")
{
obj.innerHTML = "<b>display 1 was selected</b>";
}
else if (str=="display2")
{
obj.innerHTML = "<b>display 2 was selected</b>";
}}

</script>

</head>

<body>

<form>
Display1 <input type="radio" name="States" value="display1"
onchange="ChangeContent(this.value)" />
<br />
Display2 <input type="radio" name="States" value="display2"
onchange="ChangeContent(this.value)" />

<div id="myDiv"></div>
</form>

</body>

</html>

As I said, it works perfectly in Firefox, but in IE6 I get the error:
"unknown runtime error"

Yeah. And the googled coverage is rather amazing:
<http://www.google.com/search?hl=en&q=unknown+runtime+error+Internet
+Explorer&btnG=Google+Search>
So many different explanations (some are stated as the ultimate truth
atop of it) and not a single correct one.

The problem is that the code you have posted is not the code you have
troubles with. The actual code would look something like here <http://
piecesofrakesh.blogspot.com/2007/02/ies-unknown-runtime-error-when-
using.html>

Try to notice the principal difference. If no luck then it is time to
learn the "VK's mantra" I'm too lazy to search a post, easier to type
in as it's very short and simple:

Before page load event document.write only
Ohmmm...
After page load event DOM methods only
Ohmmm...

;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top