javascript in innerHTML

A

Adriano

Dear all,

Is it possible to insert a new javascript tag through the innerHTML
(and use it of course)?
In the following example, the click on a button, insert in a div a
string (TESTO) containing a table and a javascript tag with a function
alert1.
If one tries to use alert1, a js error "alert1 is not defined" is
reported
Is there a solution to this problem?

Of course everythings is ok if the js tag is in the initial file. But I
don't need that.
Thank you for any answer.
Adriano

Example:

=============================================
FIRST.html
=============================================
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" >
<head>
<script language="javascript">
var strUserAgent = navigator.userAgent.toLowerCase();
var isIE = strUserAgent.indexOf("msie") > -1;
var isNS6 = strUserAgent.indexOf("netscape6") > -1;
var isNS4 = !isIE && !isNS6 &&
parseFloat(navigator.appVersion) < 5;

var req;

function caricaTesto (url)
{
var TESTO= "" +
"<script language='javascript'> " +
"<!-- " +
" function alert1 (url) " +
" { " +
" alert(url); " +
" } " +
"--> " +
" <\/script> " +
" " +
"<table border='1'> " +
"<tr><td>PROVONA</td></tr> " +
"<tr><td> " +
"<input type ='button' value='QUESTO BOTTONE'
onclick=\"alert1('http://172.16.0.52/testo.txt')\"> " +
"</td></tr> " +
"</table> " ;
document.getElementById("tabella").innerHTML = TESTO;
}
</script>
</head>
<body>
<h1>Request Testuale</h1>

<div id="aaa">
PROVA
<input type ="button" value="QUESTO BOTTONE"
onclick="caricaTesto('http://172.16.0.52/prova.html')">
</div>

<br><br><br><br>

<div id="tabella">
INZIO
</div>
</body>
</html>
 
O

One Dumm Hikk

Dear all,

Is it possible to insert a new javascript tag through the innerHTML
(and use it of course)?

Yes, but only in NN6.xx series browsers.
In the following example, the click on a button, insert in a div a
string (TESTO) containing a table and a javascript tag with a function
alert1.
If one tries to use alert1, a js error "alert1 is not defined" is
reported
Is there a solution to this problem?

Dynamically create script elements and then set it's .text property.
But for the code you posted, your best alternative is to simply start
over.
 
U

uoL

Hi adriano,

It doenst work :s sorry.
I dont understand quite well what r u trying to do .. but what does
work for that kind of things is iframes
for example:
with a link
<a href="script.html" target="tabella" > lala </a>
u can put a web page called for example script.html with ur script
inside inside the iframe
<iframe name="tabella" ></iframe>
only then ur javascript code is executable because this is a new web
page that initializes the code
and ur script will work fine.

hope that help
Marcos
 

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,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top