By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,682 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,682 IT Pros & Developers. It's quick & easy.

Javascript IE problem "unknown runtime error" when using innerHTML

P: n/a
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 <divelement 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"></divelement that's inside a <form></form>
element. I'm trying to change the innerHTML of the <divelement 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 <divelement 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!!!!!

Jan 13 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/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")?
"<b>display 2 was selected<\/b>" :
"";
}
</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>
</form>
</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 </bin your innerHTML ?
>>From what I could gather on the web, this is probably some kind of a
"parent" "child" problem, since the <divelement 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 ...
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jan 14 '07 #2

P: n/a
with my ie i don't get any error message.
i am using ie6, too. i haven't apply the sp yet.
sk
Jan 14 '07 #3

P: n/a
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.

Jan 16 '07 #4

P: n/a
On Jan 13, 6:36 pm, "John" <quanza....@gmail.comwrote:
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 <divelement 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"></divelement that's inside a <form></form>
element. I'm trying to change the innerHTML of the <divelement 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 <divelement 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>

Feb 7 '07 #5

P: n/a
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
}

Feb 26 '07 #6

P: n/a
VK
On Jan 13, 8:36 pm, "John" <quanza....@gmail.comwrote:
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 <divelement 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"></divelement that's inside a <form></form>
element. I'm trying to change the innerHTML of the <divelement 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...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...

;-)

Feb 26 '07 #7

P: n/a
ma**********@gmail.com wrote:
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.
Rubbish. Of course IE supports getElementById.
Feb 26 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.