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

innerHTML Javascript Issues. can anyone help?

P: n/a
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:
......................
<html>
<body>
<table>
<tr><td>
Some content here.
</td></tr>
</table>
<script language="javascript">
alert("hi");
</script>
</body>
</html>

I am assign this html page in innerHTML content

document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language="javascript">alert("hi");</script></body></html>";

when i used direct html page it's given alert message. but when i used
theu innerHTML then it's not given alert message.

how i can execute javascript in innerHTML?can anyone help?

Regards
Pratik Patel

Jun 20 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Pratik Patel wrote:
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:
.....................
<html>
<body>
<table>
<tr><td>
Some content here.
</td></tr>
</table>
<script language="javascript">
alert("hi");
</script>
</body>
</html>

I am assign this html page in innerHTML content

document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language="javascript">alert("hi");</script></body></html>";
The assignment to the innerHTML should be wrapped around with an opening
and closing quotes. I think you might overlooked that. Try this:

change language="javascript"
to language='javascript' (use single quote instead)

change alert("hi");
to alert('hi'); (use single quote instead)



>
when i used direct html page it's given alert message. but when i used
theu innerHTML then it's not given alert message.

how i can execute javascript in innerHTML?can anyone help?

Regards
Pratik Patel
Jun 20 '07 #2

P: n/a
On Jun 20, 1:47 pm, qNine <q...@comcast.netwrote:
Pratik Patel wrote:
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:
.....................
<html>
<body>
<table>
<tr><td>
Some content here.
</td></tr>
</table>
<script language="javascript">
alert("hi");
</script>
</body>
</html>
I am assign this html page in innerHTML content
document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language="javascript">alert("hi");</script></body></html>";

The assignment to the innerHTML should be wrapped around with an opening
and closing quotes. I think you might overlooked that. Try this:

change language="javascript"
to language='javascript' (use single quote instead)

change alert("hi");
to alert('hi'); (use single quote instead)
when i used direct html page it's given alert message. but when i used
theu innerHTML then it's not given alert message.
how i can execute javascript in innerHTML?can anyone help?
Regards
Pratik Patel
If i used below code.

document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language='javascript'>alert('hi');</script></body></html>";

then javascript not execute.(i don't have any alert message).

Regards,
Pratik Patel

Jun 20 '07 #3

P: n/a
Pratik Patel wrote:
On Jun 20, 1:47 pm, qNine <q...@comcast.netwrote:
>Pratik Patel wrote:
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:
.....................
<html>
<body>
<table>
<tr><td>
Some content here.
</td></tr>
</table>
<script language="javascript">
alert("hi");
</script>
</body>
</html>
I am assign this html page in innerHTML content
>
document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language="javascript">alert("hi");</script></body></html>";

The assignment to the innerHTML should be wrapped around with an opening
and closing quotes. I think you might overlooked that. Try this:

change language="javascript"
to language='javascript' (use single quote instead)

change alert("hi");
to alert('hi'); (use single quote instead)
when i used direct html page it's given alert message. but when i used
theu innerHTML then it's not given alert message.
how i can execute javascript in innerHTML?can anyone help?
Regards
Pratik Patel

If i used below code.

document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language='javascript'>alert('hi');</script></body></html>";

then javascript not execute.(i don't have any alert message).
Hi,

Javascript doesn't get executed if you just insert it somewhere.
You wrote in the PHP group your app will use AJAX.
Here too: the javascript returned by the AJAXcalls will not get executed if
you just insert it.

So how to return JavaScript in an AJAX response that gets executed?

It can be done with some parsing and eval.
But I prefer to format my AJAX response in such a way it uses predefined
routines in the 'normal' html. You'll need a little redesigning of course.

eg (AJAXresponse):
INVISIBLE myExampleDIV34
VISIBLE anotheDiv
ALERT your order is updated
**STARTHTMLLITERAL**
This is some text<br>
<b>hi</b>
<hr>
**ENDHTMLLITERAL**

etc.

Just a silly example.
Such a response is easily parsed by a simple javascript function that calls
the functions you need.

But others in here might very well have different ideas on how to approach
this.

Regards,
Erwin Moller
>
Regards,
Pratik Patel
Jun 20 '07 #4

P: n/a
If i used below code.

document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language='javascript'>alert('hi');</script></body></html>";

use:

document.getElementById('div1').innerHTML=
"<html><body><table><tr><td>Some content here.</td></tr></table>"+
"<script language='javascript'>alert('hi');<\/script></body></html>";

Jun 20 '07 #5

P: n/a
On Jun 20, 3:28 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
If i used below code.
document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language='javascript'>alert('hi');</script></body></html>";

use:

document.getElementById('div1').innerHTML=
"<html><body><table><tr><td>Some content here.</td></tr></table>"+
"<script language='javascript'>alert('hi');<\/script></body></html>";
document.getElementById('div1').innerHTML=
"<html><body><table><tr><td>Some content here.</td></tr></table>"+
"<script language='javascript'>alert('hi');<\/script></body></html>";

Above code just display "Some content here". it's not execute alert
message.

Jun 20 '07 #6

P: n/a
On Jun 20, 8:49 pm, Pratik Patel <anay.pra...@gmail.comwrote:
On Jun 20, 3:28 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
If i used below code.
document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language='javascript'>alert('hi');</script></body></html>";
use:
document.getElementById('div1').innerHTML=
"<html><body><table><tr><td>Some content here.</td></tr></table>"+
"<script language='javascript'>alert('hi');<\/script></body></html>";
document.getElementById('div1').innerHTML=
"<html><body><table><tr><td>Some content here.</td></tr></table>"+
"<script language='javascript'>alert('hi');<\/script></body></html>";

Above code just display "Some content here". it's not execute alert
message.
Many AJAX libraries provide a function to strip and eval scripts.
However, this strategy requires that you accommodate the change in
scope that occurs when eval'ing a script rather than executing it in a
global scope. A better alternative might be to load your HTML, then
once that has occurred load the script as an element and hence cause
it to be eval'd in the scope it was (most probably) intended.

Again, most AJAX libraries[1] provide an onComplete method that will
execute once the initial request has finished. That can be used to
load the required script file using DOM. Search the archives, the
following thread should get you started:

Subject: Dynamical loading of html files and executing of its
javascript content
<URL:
http://groups.google.com.au/group/co...d69d448ab20f69
>
1. <URL: http://www.ajaxtoolbox.com/ >
--
Rob

Jun 20 '07 #7

P: n/a
Pratik Patel wrote:
On Jun 20, 3:28 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
If i used below code.
>
document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language='javascript'>alert('hi');</script></body></html>";

use:

document.getElementById('div1').innerHTML=
"<html><body><table><tr><td>Some content here.</td></tr></table>"+
"<script language='javascript'>alert('hi');<\/script></body></html>";
>document.getElementById('div1').innerHTML=
"<html><body><table><tr><td>Some content here.</td></tr></table>"+
"<script language='javascript'>alert('hi');<\/script></body></html>";


Above code just display "Some content here". it's not execute alert
message.
Pratik,

Did you read my response?
I told you this wouldn't work in the way you do it...

Erwin Moller

Jun 21 '07 #8

P: n/a
On Jun 21, 3:36 pm, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
Pratik Patel wrote:
On Jun 20, 3:28 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
If i used below code.

document.getElementById('div1').innerHTML="<html>< body><table><tr><td>Some
content here.</td></tr></table><script
language='javascript'>alert('hi');</script></body></html>";
use:
document.getElementById('div1').innerHTML=
"<html><body><table><tr><td>Some content here.</td></tr></table>"+
"<script language='javascript'>alert('hi');<\/script></body></html>";
document.getElementById('div1').innerHTML=
"<html><body><table><tr><td>Some content here.</td></tr></table>"+
"<script language='javascript'>alert('hi');<\/script></body></html>";
Above code just display "Some content here". it's not execute alert
message.

Pratik,

Did you read my response?
I told you this wouldn't work in the way you do it...

Erwin Moller
Actually i want this thing need to use in external gadgets. so i don't
know what is the code in html may be javascript tag in html or may be
not. but when script tag is thr then need to execute this script. we
display external gadgets thru innerHTML.

Jun 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.