473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

innerHTML Javascript Issues. can anyone help?

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
8 3189
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Nick | last post by:
OK. I've had a trawl through past messages but can see nothing that helps me, so...here goes... I am writing a web application which, among other things, pushes a web page to a user's browser. ...
1
by: mboso | last post by:
I have a function that works in firefox, but is having some issues in IE. What it does is place an image in a specified cell, when an icon in the adjacent cell is clicked. I have to click the link...
1
by: sonic | last post by:
Hello, I am experimenting with various ways of passing content between javascript and server. Originally i was performing xsl transformation with javascript, but I noticed some performance...
3
by: ilia | last post by:
Hi All, I am a newbie in terms of Javascript, I found some code on the net to swap rows in a table using innerHTML, this works fine in Firefox but IE is complaining, after some googling around I...
4
by: tcole6 | last post by:
My problem appears to be Firefox specific. I have a hyperlink that loads a new window. This window contains hyperlinks that call javascript functions in the parent window and then closes the...
1
by: max | last post by:
Hi I want to load an external file into my page via XMLhttpRequest and innerHTML. The external file contains javascript code which is not executed during or after insertion. All the HTML markup...
5
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...
1
by: Tarik Monem | last post by:
I have been able to successfully retrieve data from an xml file, where the data has been massaged a little bit, to create a table to be retrieved and it is displayed via a document.writeln within a...
6
omerbutt
by: omerbutt | last post by:
hi there i have a registration page on which i have a captcha verification method i customized that code for the sake that the user is able to refresh the captcha images by staying on the same page...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.