0

Hi!

I am attempting to change the innerHTML code of a div and to run javascript code at that time. But I seem to be unable to do it. The actual workings of the whole thing are really convoluted but here is the general idea:

<html>
<head><title>JavaScript test</title>

<script type="text/javascript">

function changeit(iVar)
{
	var newinnerHTML = "";
	if (iVar == 1)
	{

	newinnerHTML = '<script language="JavaScript"> alert("test");</script>';		
							
	}
	else
	{
	newinnerHTML = "<a id='imagelink' name='imagelink' class='imagelink' target=_blank 

href='home.html'>Home</a>";	
	}
	document.getElementById('div1').innerHTML = newinnerHTML;
}
</script>

</head>
<body>


<body>
<div id="div1">


</div>
<form action="">
<input type="button" onClick="changeit(1);" value="change javascript" />
</form>



</body>
</html>

The end result is that I want to either show an image or play a .flv file depending upon the value of a variable. And it's all dynamic with the values coming from the database. (which is why it's too convoluted to post here)

So in a nutshell: I want to change the javascript on a page using javascript and have it run.

Any help would be greatly appreciated!

-Karin

2
Contributors
4
Replies
8
Views
7 Years
Discussion Span
Last Post by karin42
0

Hi, thanks for the response. Please don't worry about the structure of this document that I posted, This is just an example of what I'm trying to do. The question is actually:

How do I change the javascript on a page using javascript and have it run.

Thanks!

-Karin

0

I tested out your code and it came to the conclusion that it can't work. Due to the fact that the browser has already read the javascript and sees the innerHTML as normal html and not a script that needs to be executed. Also the <script language="JavaScript"></script> interruptes the javascript. I tested it with the following version:

<html>
<head><title>JavaScript test</title>

<script type="text/javascript">
<!--
function changeit(iVar)
{
	var newinnerHTML = "";
	if (iVar == 1)
	{

	newinnerHTML = '<script type="text/javascript">alert("test");</script>';		
							
	}
	else
	{
	newinnerHTML = "<a id='imagelink' name='imagelink' class='imagelink' target='_blank' href='home.html'>Home</a>";	
	}
	document.getElementById('div1').innerHTML = newinnerHTML;
	alert(document.getElementById('div1').innerHTML);
}
//-->
</script>

</head>
<body>
<div id="div1">
<script type="text/javascript">alert("test");</script>
</div>
<form action="">
<input type="button" onClick="changeit(1);" value="change javascript" />
</form>
</body>
</html>

And it doesn't work. However, when you want to display an image, it does work.

~G

0

Thank you Graphix, I figured out the same thing. It would've been cool to be able to reload just a bit of Javascript code but oh well.

I redesigned what I was doing so now the question is gone.

Thanks!

-Karin

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.