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

Home Posts Topics Members FAQ

How To Insert Code With Javascript, How to insert into a div an amountof code

i,
I have the next html page
<html>
<head>
<script>
<!--
function insertcode()
{
var code ="<p> blablabal babala babababab</p><h1>here comes
header</h1><span>fadfaf a<a href=\"fadfaf\" >anchor</a>blalbababa</span>"
var myText = document.create TextNode(code);
document.getEle mentById("conte nt").appendChil d(myText);
}
-->
</script>
</head>
<body>
<div id="content">
</div>
<a href="javascrip t:insertcode()" >Insert Code</a>
</body>
</html>

This code insert the data as text. The html tags are not treated like
markup. I need to insert the code in a time. I mean i CAN NOT go tag per
tag. (E.g. document.create Element("p")... )
Is there any method to insert a pice of html code into a div and keep it
like code not like text in Javascript?
I hope you understand my problem. If not please tell me. Thanks in
advanced for your help.

Jul 23 '05 #1
4 6305
"Sergio del Amo" <se***@sbox.tug raz.at> wrote in message
news:42******** *************** @aconews.univie .ac.at...
i,
I have the next html page
<html>
<head>
<script>
<!--
function insertcode()
{
var code ="<p> blablabal babala babababab</p><h1>here comes
header</h1><span>fadfaf a<a href=\"fadfaf\" >anchor</a>blalbababa</span>"
var myText = document.create TextNode(code);
document.getEle mentById("conte nt").appendChil d(myText);
}
-->
</script>
</head>
<body>
<div id="content">
</div>
<a href="javascrip t:insertcode()" >Insert Code</a>
</body>
</html>

This code insert the data as text. The html tags are not treated like
markup. I need to insert the code in a time. I mean i CAN NOT go tag per
tag. (E.g. document.create Element("p")... )
Is there any method to insert a pice of html code into a div and keep it
like code not like text in Javascript?
I hope you understand my problem. If not please tell me. Thanks in
advanced for your help.

Will this help? Watch for word-wrap.

<html>
<head>
<title>inner.ht ml</title>
<script type="text/javascript">
function insertcode() {
var code ="<p> blablabal babala babababab</p>";
code += "<h1>here comes header</h1>";
code += "<span>fadfafa< a href=\"fadfaf\" >anchor</a>blalbababa</span>";
document.getEle mentById("conte nt").innerHTM L = code;
}
</script>
</head>
<body>
<div id="content">
<a href="javascrip t:insertcode()" >Insert Code</a>
</div>
</body>
Jul 23 '05 #2
> "Sergio del Amo" <se***@sbox.tug raz.at> wrote:
news:42******** *************** @aconews.univie .ac.at....

<script>
<!--
function insertcode()
{
var code ="<p> blablabal babala babababab</p><h1>here comes
header</h1><span>fadfaf a<a
href=\"fadfaf\" >anchor</a>blalbababa</span>" var myText =
document.create TextNode(code);
document.getEle mentById("conte nt").appendChil d(myText); }
-->
</script>


<script>
<!--
function insertcode()
{
var code ="<p> blablabal babala babababab</p><h1>here comes header</h1><span>fadfaf a<a
href=\"fadfaf\" >anchor</a>blalbababa</span>"
document.getEle mentById("conte nt").innerHTM L = document.getEle mentById("conte nt").innerHTML+ code
}
-->
</script>

--
BootNic Monday, May 23, 2005 2:21 PM

If we were not meant to make mistakes we would not have a Department of Corrections.
*Unknown**
Jul 23 '05 #3
Sergio del Amo wrote:
I have the next html page
You mean a HTML _document_ which may be displayed as many pages and
which is invalid, BTW:
<html>
The DOCTYPE declaration is missing before that tag.

<http://validator.w3.or g/>
<head>
<script>
The `type' attribute is missing.
<!--
Scripts do not need and should not to be commented out this way.
function insertcode()
{
var code ="<p> blablabal babala babababab</p><h1>here comes ^^
The `script' element is considered to end here in SGML, so
you have to escape ETAGO delimiters within CDATA content.
header</h1><span>fadfaf a<a href=\"fadfaf\" >anchor</a>blalbababa</span>"
var myText = document.create TextNode(code);
1. You have not tested for the existence of the host method
document.create TextNode() before calling it. This is error-prone.

<http://pointedears.de/scripts/test/whatami>

2. This will create a _text node_, not child elements. If you still want to
create child elements that way, you can use the proprietary `innerHTML'
property; however, I strongly recommend to use standardized DOM methods
instead.
document.getEle mentById("conte nt").appendChil d(myText);
Same here: Feature tests have not been performed prior.
} -->
This is a syntax error as `--' is the decrement operator, and `>'
is not a valid operand. Remove that line, you don't need it.
</script>
</head>
<body>
<div id="content">
</div>
<a href="javascrip t:insertcode()" >Insert Code</a>
Don't use the proprietary `javascript:' URI scheme, use standardized event
handlers, here: onclick. Note that this "link" does not work without
client-side script support, so you better also include it using scripting.
</body>
</html>

This code insert the data as text. The html tags are not treated like
markup.
See above: works as designed.
I need to insert the code in a time.
Why?
I mean i CAN NOT go tag per tag. (E.g. document.create Element("p")... )


Why not? Laziness?
PointedEars
Jul 23 '05 #4
DU
Sergio del Amo wrote:
i,
I have the next html page
<html>
Your document has no doctype declaration.
<head>
<script>
You're missing type attribute here.
<!--
function insertcode()
{
var code ="<p> blablabal babala babababab</p><h1>here comes
header</h1><span>fadfaf a<a href=\"fadfaf\" >anchor</a>blalbababa</span>"
var myText = document.create TextNode(code);
document.getEle mentById("conte nt").appendChil d(myText);
}
This is definitively not best and not correct. If you want to
dynamically insert a block of markup code, then you don't need and
certainly should not create a text node which is markup code: it's
incoherent. Also, as coded, your insertcode function will fail markup
validation. Best is to use DOM 1/2 attributes and methods.
How about:

function InsertCode()
{
if(document.cre ateElement && document.append Child)
{
var objParg = document.create Element("p");
objParg.appendC hild(document.c reateTextNode(" blablabal babala babababab"));
var objH1 = document.create Element("h1");
objH1.appendChi ld(document.cre ateTextNode("he re comes header"));
var objSpan = document.create Element("span") ;
objSpan.appendC hild(document.c reateTextNode(" fadfafa"));
var objLink = document.create Element("a");
objLink.href = "fadfaf"; /* obviously, this will require a real working
value */
objSpan.appendC hild(document.c reateTextNode(" anchor"));
objSpan.appendC hild(objLink);
objSpan.appendC hild(document.c reateTextNode(" blalbababa"));
document.getEle mentById("conte nt").appendChil d(objParg);
document.getEle mentById("conte nt").appendChil d(objH1);
document.getEle mentById("conte nt").appendChil d(objSpan);
};
}

Not tested but I've done often similar code in MSIE 6, Opera 7+, Firefox
1.x, Netscape 7+ and Mozilla-based browsers before and I am almost 100%
sure it will work in those mentionned browsers.
-->
</script>
</head>
<title></title> is mandatory.
<body>
<div id="content">
</div>
<a href="javascrip t:insertcode()" >Insert Code</a>
Using "javascript :" pseudo-protocol is always wrong, incorrect unless
you want to create a bookmarklet.
http://jibbering.com/faq/#FAQ4_24
Protocol scheme "javascript :" will be reported as an error by link
validators and link checkers.
</body>
</html>

This code insert the data as text.
And that data text is markup code...

The html tags are not treated like markup. I need to insert the code in a time. I mean i CAN NOT go tag per
tag. (E.g. document.create Element("p")... )
Why not? This is what I would recommend and what I personally would do.
Is there any method to insert a pice of html code into a div and keep it
like code not like text in Javascript?
innerHTML is a shortcut but that isn't in the DOM specs.
I hope you understand my problem. If not please tell me. Thanks in
advanced for your help.


I understand well your problem. But I must say that your html code could
be improved a lot.

DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Firefox 1.0.4 :)
Jul 23 '05 #5

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

Similar topics

2
13391
by: george | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, on an NT box, Active Server pages with Javascript, using ADO objects. I'm inserting simple records into a table. But one insert command is placing 2 or 3 records into the table. The 'extra' records, have the same data as the previous insert incident, (except for the timestamp).
6
4025
by: Stefan Mueller | last post by:
The following code (HTML) generates a table. Now I'd like to insert a new row by a javascript. The following code (javascript) works with the Internet Explorer and also with Mozilla. However, the inserted button (onClick) in the table does not work with the Internet Explorer. It only works with Mozilla. I'm trying the whole Sunday without any success. Please give me a hint. Stefan
2
1551
by: reidarT | last post by:
I have 3 fields in an aspx page. The 3. field should be the sum of field A and field B I use OnTextChanged to calculate the sum in field3. At the same time I want to insert the content of theese 3 fields into a row in a table. The problem is that I need 'postback is true' on the textfields to get an immediate calculation and then the insert action is triggered. reidarT
1
6067
by: bodilima | last post by:
Dear All, I'm new to this! I wanna insert selected values from form "SaveModel" to form "SaveMake" <input name="makeID" value="<<<<need to insert here(x)>>>>"> & <input name="make" value="<<<<need to insert here(optvalue)>>>>"> when i try it works only for the selected value and in an instance it clears the <input...>.
11
15275
by: TechnoAtif | last post by:
INSERT AND UPDATE MULTIPLE CHECKBOX DATA USING PHPMYSQL OR JAVASCRIPT Hi All I want to check the multiple checkboxes update them after revisiting that page. I am taking the name as name=type..in the form of array..but on checking it the result in the database simply gives the world 'array'.i want multiple checkboxes to be checked and updated simultaneously.. and the query to insert the checkbox value has to be included along with...
2
3197
by: adamace5o | last post by:
When i try to use post variables with php and mysql i can't get the insert into statement to accept varibles as values. If i use 'test' instead of $test it does work. I suspect it is something to do with the javascript im using but i can print the correct values so why am i unable to use them to enter data to a database? sorry i am new to all this here is the updatedata.php file <?php session_start(); $host="localhost"; // Host...
9
2712
by: sunita jadhav | last post by:
my question is if i type in html textbox on key press event suppose i type 12345 values in textbox then i delete or edit any value of text box suppose i edit 3 and i insert the value 6 at 3 but i have face problem that is if i insert 6 at 3 this value is not insert at 3 it insert last positon i.e after last value .In given example the value is appned or insert after 5 then output is 12456 but i have want the output 12645 and i also give the...
58
8064
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also the parts section an i seem to be having trouble. When i try to insert into the parts section i get the error Invalid character value for cast specification. But not sure what i am doing wrong. Here is what i am using to insert. All the sections...
1
2521
by: ghjk | last post by:
I'm having php page with validation. This is the code. <script language="javascript" src="../javascripts/validate.js"></script> <script type="text/javascript"> function valForm(){ if(!valid_required(document.add.FullName.value)) { add.FullName.style.background = 'Yellow'; alert("First name is required field.")
0
8315
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
8829
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
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...
1
8508
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8608
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
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
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
2
1962
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.