Jump to content
xisto Community
mizako

How To Insert Code With Javascript How to insert into a div an amount of code

Recommended Posts

Hi,
I have the next html page

<html><head><script><!--function insertcode(){var code ="<p> blablabal babala babababab</p><h1>here comes header</h1><span>fadfafa<a href=\"fadfaf\">anchor</a>blalbababa</span>"var myText = document.createTextNode(code);document.getElementById("content").appendChild(myText);}--></script></head><body><div id="content"></div><a href="javascript: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.createElement("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.

Share this post


Link to post
Share on other sites

I couldn't understand what you were trying to do. I think you are trying to place another HTML/text page in your HTML page. I saw this code somewhere in this forum but I cannot find who wrote it at this time. A apologize to the author for posting without crediting you. I will come back and correct it once I find it. (or someone PM me if you find it)

<object type="text/x-scriptlet" width=100% height="250" data="Your Html File(TextFile )"></object>

This simple liner, I was told, that allows you to place another html/text page into your html page. It's a document within the document.I'm telling ya, we got some brilliant codeboys here!

Share this post


Link to post
Share on other sites

Is there any method to insert a pice of html code into a div and keep it like code not like text in Javascript?


Why is it necessary to insert it as HTML instead of javascript? It should still validate OK. And why are you using javascript to insert the code in the first place? Why not just type it out? :lol:

Share this post


Link to post
Share on other sites

Why is it necessary to insert it as HTML instead of javascript? It should still validate OK. And why are you using javascript to insert the code in the first place? Why not just type it out?  :lol:

142540[/snapback]


Ok, i will try to explain myself better. Basically i am working on a Remote Scripting using hidden frames (i will explain what it is this in another topic). project. I have a database in MySQL where in one table i have a biography in XHTML. I access this table with PHP and i retrive this XHTML code. Then the Javascript function obtains the code and has to insert it into a this "div" element. As i explain in my first post. ;-)

I know the mechanismus is complicated but i have to do it in this way.

I hope you both understand better what i am asking for. I did not understand your solution BuffaloHELP but thanks both of you for your answers.

Does anyone knows one can i solve this Javascript Issue?

Share this post


Link to post
Share on other sites

Ok, i will try to explain myself better. Basically i am working on a Remote Scripting using hidden frames (i will explain what it is this in another topic).  project. I have a database in MySQL where in one table i have a biography in XHTML. I access this table with PHP and i retrive this XHTML code. Then the Javascript function obtains the code and has to insert it into a this "div" element.

 

You don't need javascript to write out the code, you can use PHP. You're using just about the same language anyway and you're already using PHP to retrieve the info from the database. It seems logical that you would then use PHP to just print it out using echo or print. You could use PHP code to print your whole page if you wanted to:

 

<?php print "<head>";print "<title>Your title</title>";print "<body>";
etc.

Share this post


Link to post
Share on other sites

You don't need javascript to write out the code, you can use PHP. You're using just about  the same language anyway and you're already using PHP to retrieve the info from the database. It seems logical that you would then use PHP to just print it out using echo or print. You could use PHP code to print your whole page if you wanted to:

 

As i wrote in my second message i am trying to implement Remote Scripting which aims to reduce the code (avoiding redundant markup) sent by the server to the client. That it is to say that the PHP scripts just send plain text to the client and the client creates the interface by means of Javascript. I know PHP and i know that i can write the whole page with PHP but i need to do it by Javascript. I did not want to begging a topic about Remote Scripting in this thread (maybe in another one), i just wanted to obtain a Javascript solution for the problem i exposed. I appreciate a lot your help but please answer me with a Javascript solution if you know it.

Thanks in advanced!

Share this post


Link to post
Share on other sites

Hello,I found the solution :-) . Somebody point me to the "innerHTML" method. The Code with the included solution is the next:

<html><head><script><!--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.getElementById("content").innerHTML = code;}--></script></head><body><div id="content"></div><a href="javascript:insertcode()">Insert Code</a></body></html>

Notice from BuffaloHELP:
Entered
 tags.
Edited by BuffaloHELP (see edit history)

Share this post


Link to post
Share on other sites
dynamic script tagHow To Insert Code With Javascript

I need to insert a script tag dynamically which calls google search api...The problm is  that I cant find a way to insert the source of the script tag that call thaat search api :S

can anyone help pls?

-reply by mmmrt

 

Share this post


Link to post
Share on other sites

use the innerHTML property to insert markup.  This is an IE invention but is supported in Firefox and Opera as well.  It's not pretty, and if you're inserting something fixed you should probably create the markup properly (using createElement, createTextNode etc...), but when you're inserting someone elses text/markup it seems to work!

-reply by sabroni

 

Share this post


Link to post
Share on other sites
HTML as objectHow To Insert Code With Javascript

Hi :)

Being a novice in all the webdesign variations, I came upon a problem that might be very basic (sorry about this) but I've tried everything I could think of and still not closer to a solution.

Here's my problem:

I'm building a website that was requested to be formatted through tables.As part of the site, of course, there's a javascript dynamic accordion style menu.Due to the dimension of the web site (somewhere close to 300 pages butDue to increase 5 to 10 pages every forthnight), I didn't want to haveThe script in each page as the menu will be altering continuously, soCreated "menu.Htm" and pulled it into every page as an object.

<object type="text/x-scriptlet" border="0" width=200 height=650 data="menu.Htm">

So far so good, and while working from my disc all was working great.When the site was uploaded to the company's (foreign) server, IEDoesn't show the menu at all and Firefox opens the menu but all theLinks cointained in it open in the original object's bounding box.Before you all laugh, I'm trying to help my partner look good to hisBoss, I'm not a programmer and HTML was a kick when I was inHigh-school doing marquees for fun :(I might have to add some attribute to the "a" tag in the menu, but if so don'tknow which...

 Any ideas welcome, thanks for helping me

Share this post


Link to post
Share on other sites

Problem is your code won't work if you use "</script> anyware in your "code" variable.For example :Var code "<script>alert('Now I'm broken');</script>" Don't ask me for an answer which works on this one, I've seen plenty try, but all answers escape the <script> tags which makes them useless. Fine if you're trying print the <script> tag to screen, not if you're trying to make it do something.  Sorry for responding to an old post, but just thought I might save someone the headache. 

 

Share this post


Link to post
Share on other sites

Hi,I have the next html page

<html><head><script><!--function insertcode(){var code ="<p> blablabal babala babababab</p><h1>here comes header</h1><span>fadfafa<a href=\"fadfaf\">anchor</a>blalbababa</span>"var myText = document.createTextNode(code);document.getElementById("content").appendChild(myText);}--></script></head><body><div id="content"></div><a href="javascript: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.createElement("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.

This function is really useful...

but this is for simple textareas. If you override textarea with tinymce text editor class. This function returns error.

Can someone suggest the possible solution to work with tinymce or or any other editor with textarea.

Quick reply would really be appreciated.

Thanks

Hemant

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.