473,385 Members | 1,720 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,385 software developers and data experts.

Can Javascript work with Multiple lines of Text.

Hello,

Another newbie question here.

Let me explain my situation first. I have bought a 3rd party tool
that runs a PHP script and gives me some HTML code which I can
directly use in my pages.

The code generated is normal HTML code, example

<<<<<<<<<<<<Example>>>>>>>>>>>>>>>>>>

<table class= "myclass" border="1" width="160" cellspacing="0"
cellpadding="0">
<tr>
<td width="100%">
<table border="0" width="160" cellspacing="0" cellpadding="1"><tr>
<td width="55" valign="top" align="center">

<<<<<<<<<<<<Example Ends>>>>>>>>>>>>>>

Now all the above code is contained in a PHP variable. When I want to
display the above code on my site, all I have to do is insert

%%Variable_Name%%

and it will embed the whole code on my webpage.

Now here's what I want. I need to make some changes in the above HTML
code using Javascript. In particular I want to search for some value
and make replacements.

Since the above code contains text spread across multiple lines, I
don't know whether it would work with Javascript. All I want to do is
put the above code (text) in a Javascript variable and make some
changes. I tried but it is not working.

I contacted the developer but he says that since his tool is being
used by large number of people he can't make changes based on just one
request.

Any ideas on how to proceed ahead..

Thanks in advance,
Rayne
Sep 25 '08 #1
3 4068
ja***********@gmail.com meinte:
Since the above code contains text spread across multiple lines, I
don't know whether it would work with Javascript. All I want to do is
put the above code (text) in a Javascript variable and make some
changes. I tried but it is not working.
Tried what? What is "not working"? Show either your efforts, or make
clear what you want to achieve.

Gregor

--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Sep 25 '08 #2
On Sep 25, 6:50*pm, jackson.ra...@gmail.com wrote:
>
Since the above code contains text spread across multiple lines, I
don't know whether it would work with Javascript. All I want to do is
put the above code (text) in a Javascript variable and make some
changes. I tried but it is not working.
An idea:
javascript:txt= document.documentElement.innerHTML;alert(txt);

or two:
javascript:txt= document.body.innerHTML;alert(txt);

But I think you're heading in the wrong direction...
--
Jorge.
Sep 25 '08 #3
On Sep 25, 12:50*pm, jackson.ra...@gmail.com wrote:
Hello,
Hello.
>
Let me explain my situation first. I have *bought a 3rd party tool
that runs a PHP script and gives me some HTML code which I can
directly use in my pages.

The code generated is normal HTML code, example
[snip]

Ok. Understand what's first, first.

1) HTML = HyperText Markup Language; PHP = PHP Hypertext Processor. So
here we go:

If you have the PHP extension installed unto a web server, it means
that the server, as it reads the requested file from the client, that
file will be caught by the PHP parser and execute any script it finds,
replace the script portion of the file by whatever output it produces,
and the server will send that portion instead of the PHP script. (This
is the same thing with any other server-side scripting language. (I'm
explaining briefly here.)

For example, if your equest the file hello.php, and the file looks
like this:

<html>
<head>
</head>
<body>
<?php echo '<p id="hello">Hello world!</p>'; ?>
</body>
</html>

You will receive:

<html>
<head>
</head>
<body>
<p id="hello">Hello world!</p>
</body>
</html>

Now, PHP doesn't understand the HTML part of you page. In fact, it
doesn't know it at all. All it knows (in this example anyway) is that
there's a string that contains '<p id="hello">Hello world!</p>', and
that it outputs it. Thus, PHP can replace portion of the strings it
knows BEFORE it sends it to the client. But if you want to modify the
HTML only from the client side, THEN you make use of Javascript. So,
let's change the code a little:

<html>
<head>
</head>
<body>
<?php echo '<p id="hello">Hello world!</p>'; ?>
<script type="text/javascript">
var el = document.getElementById("hello");
el.innerHTML += "<br />Bye world!";
</script>
</body>
</html>

You will receive from the server this file:

<html>
<head>
</head>
<body>
<p id="hello">Hello world!</p>
<script type="text/javascript">
var el = document.getElementById("hello");
el.innerHTML += "<br />Bye world!";
</script>
</body>
</html>

NOTE : the <scripttag is still there! That's because it has not yet
been executed! But first, the browser (IE/Firefox/etc.) has to parse
that data too. This is because HTML is markup language, and the
browser converts the markups into the DOM (Data Object Model); the DOM
is organized in a tree-like structure in the memory of the browser.
So, this is why everything is attached to the document object in
javascript. (Because Javascript talks to the DOM, not the HTML.) In
your example, the DOM would look like this:

object:window
...DOMElement:body
.....DOMElement:p{id:"hello"}
.......textElement:Hello world!

NOTE : this is a very simplified representation of the DOM tree, for
explanation purposes.

As you may have noticed, the <scripttag is not there. This is
because, as PHP scripts are executed as they are encountered,
Javascript is the same thing. The browser executes the scripts, and
store whatever it tells it to store in memory. In our example, the
browser now has to parse the <scripttag of the file it received,
having the DOM tree above. When executed, the script makes a requests
to the browser to find the element with an id of "hello". The Browser
returns the <pelement with every nodes it contains (here, it's a
text node "Hello world!"

NOTE : if a script request an element that has not yet been added to
the DOM, the browser will return 'undefined'. This is why you cannot
directly access elements below the <scripttags.

Then, the script tells the browser to add HTML content to this <p>
element. The DOM now becomes :

object:window
...DOMElement:body
.....DOMElement:p{id:"hello"}
.......textElement:Hello world!
.......DOMElement:br
.......textElement:Bye world!

Et voilà!

NOTE : when you use innerHTML in your Javascript, the browser has to
parse the data set to the element. If you can read the value, it's
simply because the browser holds the string that it used to parse the
data when it received the file from the server. It doesn't really use
the data itself for processing beyond the parsing.

In conclusion, the best way to replace data in your page is :

1) the the parent element of your <tableand get it's innerHTML, then
modify it. (Remember, every time you change innerHTML, the browser has
to parse it, so I suggest you use a variable to do what you want, then
reassign the value to the innerHTML.)

2) directly get the elements you want with the DOM methods :
document.getElementById(id) or document.getElementsByTagName(tag) (the
last method returns an array regardless. Be advised!)

3) modify the PHP strings before the script outputs them to the
client.
I hope this was clear enough, and helpful. :)

yanick
Sep 26 '08 #4

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

Similar topics

2
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
2
by: techfuzz | last post by:
I scoured this group and others looking for the best way to disable a button after the first click to prevent multiple submissions, but never did find anything that worked like they said it would. ...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
10
by: Tom Cole | last post by:
While I've done javascript work for as long as I can remember (since Netscape first released it), I've only ever used it for trivial things, change a color here, validate a text element there, blah...
5
by: CartmanCreative | last post by:
I am totally new to Javascript, and would desperately like some help. I have downloaded a Javascript Text Scroller that uses DHTML to scroll text up and down. There are other Java scrollers that do...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.