473,657 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript + InnerHTML problem

Hi

I have a problem, pls see below:

<html>
:
<script type="text/javascript">
<!--
function addCode () {
document.getEle mentById('empty Space').innerHT ML+=
"<b>This is a test part </b><script
language='javas cript'>window.a lert('Hi');</script>";
}
// -->
</script>

:
<center id="emptySpace" ></center>
<a href="javascrip t:addCode()">Ad d Code</a>
:
</html>

The problem is when I clicked on the link, only the text "This is a
test part" was displayed. The alert box was not appear.

Please help me, thanks.

Aug 9 '06 #1
4 8841
Hi all,

i changed

<script language='javas cript'>window.a lert('Hi');</script>

to

<script defer>window.al ert('Hi');</script>

and it works ....

but only in IE but not in Mozilla Firefox....

Please help me...Thanks.

Aug 9 '06 #2

Henry wrote:
Hi

I have a problem, pls see below:

<html>
:
<script type="text/javascript">
<!--
Don't use HTML comments inside script elements.

function addCode () {
document.getEle mentById('empty Space').innerHT ML+=
"<b>This is a test part </b><script
language='javas cript'>window.a lert('Hi');</script>";
}
Don't allow posted code to auto-wrap, wrap it manually at about 70
characters. Inserting script elements using innerHTML is problematic
since there is no public standard on how it should behave.

innerHTML is a proprietary Microsoft invention that has been widely but
inconsistently copied - you shouldn't rely on it.

// -->
</script>

:
<center id="emptySpace" ></center>
The center element is deprecated in HTML 4.

<a href="javascrip t:addCode()">Ad d Code</a>
:
</html>

The problem is when I clicked on the link, only the text "This is a
test part" was displayed. The alert box was not appear.
Put the script in an external file. Create the script element using
document.create Element then assign an src attribute with a value that
points to the external file. There are lots of examples in the
archives.

file: a.js
==========

alert('hi');

file: a.html
============

<title>load script test</title>
<script type="text/javascript">

function loadScript(){
var oScr = document.create Element('script ');
oScr.type = 'text/javascript';
oScr.src = 'a.js';
document.body.a ppendChild(oScr );
}

</script>
<input type="button" value="Add script" onclick="loadSc ript();">

--
Rob

Aug 9 '06 #3
"RobG" <rg***@iinet.ne t.auwrote in comp.lang.javas cript:

innerHTML is a proprietary Microsoft invention that has been widely but
inconsistently copied - you shouldn't rely on it.

I agree. So how does one convert a string of HTML to a DOM document
fragment for appending?

I have my own code, but I'll bet it's bug-ridden.

Aug 9 '06 #4

Patient Guy wrote:
"RobG" <rg***@iinet.ne t.auwrote in comp.lang.javas cript:

innerHTML is a proprietary Microsoft invention that has been widely but
inconsistently copied - you shouldn't rely on it.


I agree. So how does one convert a string of HTML to a DOM document
fragment for appending?
The obvious answer is don't design your application to use HTML
fragments. :-)

It is probablly OK to inject HTML where you are getting it from a
server (e.g. AJAX) in small chunks that you know work, however it
doesn't make much sense to use it for script elements, where logic
dictates the types of elements you are creating (or their attributes)
or where you know it is inconsistent across browsers.

IE is quite slow using DOM methods compared to innerHTML, but Firefox
isn't. I expect IE 7 to be much more efficient.

I have my own code, but I'll bet it's bug-ridden.
Check out the DOM 3 Load and Save spec, though at present IE lacks
useful support (again, IE 7 may change that but I don't think it will)
and Firefox's support is limited.
--
Rob

Aug 9 '06 #5

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

Similar topics

4
3831
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following beautiful script "http://javascript.internet.com/navigation/toolbar-menu.html". It works like a charm. I made my webpage in frames, where the nav-frame shows the menubar, so whenever I click a link in the menubar, it opens in the frame below. But...
5
1533
by: Robin Becker | last post by:
I'm trying to fix a problem in IE 5.2 javascript on mac OS 10.2. Basically the problem is that we're writing dynamic form elements and these don't seem to be recognized by the form. The code that does the writing looks like /////////////////////////////////////// var IE4 = document.all ? 1 : 0; var NS4 = document.layers ? 1 : 0; var DOM = (!IE4 && document.getElementById) ? 1 : 0;
7
1675
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 they want to view/print and a little jscript .innerHTML magic happens under the hood that copies...
7
38002
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 situation, and since my experience with JavaScript is limited, I was not able to adapt the solutions I...
5
2111
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:
3
1825
by: BeginnerHyun | last post by:
A question on "innerHTML". Hi, The below is my HTML code. ActiveX function "GetData()" returns HTML string which may include javascript codes. This HTML string may be HTML codes in any pages which I will navigate to. The result of the below code is not what I expected. Error occurred in the line "divEncId.innerHTML=enc;".
4
2708
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 if qty equall to or greater than 2 and equall to or less than 9 then price =
16
1927
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi All, I have a neat little script that calculates price based on quantity without refreshing the page.. the script is - <script type="text/javascript"> function OpenWin(url) { window.open(url,'win','scrollbars=1,status=0,resizable=0,width=200,height=265'); }
1
1342
by: Adrienne Boswell | last post by:
Gazing into my crystal ball I observed =?Utf-8?B?R1ROMTcwNzc3?= <GTN170777@discussions.microsoft.comwriting in news:443E2509-7F0E-4CEC-B243-D6EDC931DB7F@microsoft.com: ASP has no knowledge of the client, this is a client side issue. Followups set to comp.lang.javascript. 0,widt
5
2494
by: Maize | last post by:
Hi Guys, i'm having a problem with a little Javascript in a link Sorry that it is all in a line but it is actually a link. Please wrap. This results in a new page with only "Initially processing" showing <a href="javascript:var Aobj = document.getElementsByTagName('span'); for (var i = 0;i &lt; Aobj.length;i++) { if (Aobj.className === 'SLA') { Aobj.innerHTML += 'Initially processing'; Aobj.innerHTML = ''; } }"><br/>Read more</a>
0
8392
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8726
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.