innerHTML Javascript Issues. can anyone help?

P

Pratik Patel

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
 
Q

qNine

Pratik said:
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)
 
P

Pratik Patel

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)

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
 
E

Erwin Moller

Pratik said:
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
 
S

scripts.contact

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>";
 
P

Pratik Patel

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.
 
R

RobG

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/c...+script+element&rnum=1&hl=en#95d69d448ab20f69
1. <URL: http://www.ajaxtoolbox.com/ >
 
E

Erwin Moller

Pratik said:
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
 
P

Pratik Patel

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.
 

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
474,159
Messages
2,570,885
Members
47,419
Latest member
ArturoBres

Latest Threads

Top