473,536 Members | 2,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript in innerHTML

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>

Oct 13 '06 #1
2 3945


On Oct 13, 3:59 am, "Adriano" <adip...@gmail.comwrote:
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.

--
Randy

Oct 13 '06 #2
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

On Oct 13, 4:59 am, "Adriano" <adip...@gmail.comwrote:
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>
Oct 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4556
by: VictorG | last post by:
Hello, I am new to JS and am trying to add some HTML into a JS function. So that when called the script as well as the HTML will be invoked. Is there some type of embed mechanism, sort of the reverse of embedding JS in an html page with the script tag. (which is what I am doing in this case) Is this even possible?
9
2098
by: Astra | last post by:
Hi everybody Wonder if you could help me out. I created a simple JavaScript routine to enable a user to click backwards and forwards between small news articles. This routine works fine in IE and Safari, but in the latest FireFox I get no title or article, but do see the prev and next links and the article number. My HTML and JS...
7
1664
by: e | last post by:
I've been having an extremely difficult time finding an answer to this in IE / js groups, so I thought I'd try here. I've got an aspx page that delivers loads of report data into custom-named <span> tags on the client, hidden from the screen by @media classes. From a dynamically built menu of what was returned, the user selects wich report...
7
3192
by: julian.tklim | last post by:
Hi, I need to build an editable Datagrid with add & delete buttons on each row using javascript. DataGrid need not be pre-populated with values. To make the thing complicated, one of the column need to be a date picker field. I know things will be easier with ASPX datagrid.
7
37997
by: John | last post by:
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...
22
2284
by: Christopher Nelson | last post by:
I have a little menu system which essentially takes HTML like: <div id='foo'></div> and retrieves foo.shtml from the server and inserts it inside the <div>. But sometimes I'd like foo.shtml to look like: <script language='JavaScript'> ...do something AJAX-y </script>
3
2577
by: WayneH | last post by:
Hi - I'm trying to use javascript to determine if a user's browser has cookies enabled or not. To test: copy this code into a file with a 'html' extension, and load it into your IE browser directly, through a local web server and through a remote web server. By turning cookies on/off in your browser, running the file, then clicking the...
8
3203
by: Pratik Patel | last post by:
Hello, I used innerHTML to assign HTML content. but in my HTML page content have also some javascript function and it will run when page load. bu when HTML code assgin thru innerHTML then this javascript is not working. e.g. html content:
2
1713
by: smartic | last post by:
i'm having three lists when i select from any one the others be visible by hierarchy but it takes to long to write my code is there is another away to write this code like XML that is my javascript code ex: function ChangeCategory(ForWhat){ switch(ForWhat.selectedIndex){ case 0: //if he selected noothing...
4
2691
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi Guys, thanks for your help yesterday, I've got one more question, then I think I'm done for now,... Is it possible to insert recordset data in a javascript, for instance I have a javascript code that calculates the total price, depending on number of units, currently what the code does is set the price like so - if qty 1 then £99+VAT...
0
7359
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7289
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7680
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7273
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
918
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.