By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

variables as Verbatim Strings. Pls help

P: n/a
hi all,

I have a variable, let's call it xyz, as a string. I want xyz to be a string
which I copied from a website that has a lot of back slashes, forward
slashes, single quotes, double quotes....

var xyz = new String();
xyz = " This is a long long text with a lof of weird characters that need
escape character"
So in order to construct a proper xyz, one way I know is to use
concatenation and A LOT of
escape character.

My question therefore is:
Is there any way to get around this?

I know in PHP you have something like this (may syntactically wrong)

var xyz = <<< Orginal text >>>

Anyone have any idea please help me! I will appreciate any reply. Thanks

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Cortes" <tv*********@yahoo.com> writes:
I have a variable, let's call it xyz, as a string. I want xyz to be a string
which I copied from a website that has a lot of back slashes, forward
slashes, single quotes, double quotes....

var xyz = new String();
This assignment is completely superflous. It does nothing except create a
String object (something you will probably never need to do) and store
it in a variable that is then immediately overwritten. Just:
var xyz;
will do.
xyz = " This is a long long text with a lof of weird characters that need
escape character" So in order to construct a proper xyz, one way I know is to use
concatenation and A LOT of escape character.
That will work. What is the problem with that?
My question therefore is:
Is there any way to get around this?
There is no simpler way to enter strings than as string literals.
What you can do, depending on where your code needs to be used, is
to have the text as part of an HTML page and use DOM methods to extract it:

<div style="display:none;" id="hiddenDiv">
Very" long' string! of \something foo'bar'ed.
</div>
<script type="text/javascript">
var xyz = document.getElementById("hiddenDiv").firstChild.no deValue;
</script>

Be aware that some browsers will trim the surrounding whitespace around
the text.
I know in PHP you have something like this (may syntactically wrong)

var xyz = <<< Orginal text >>>
More likely (IIRC):

$xyz = <<EOT
original text
EOT
Anyone have any idea please help me! I will appreciate any reply. Thanks


There is no "here text" constuction in Javascript, like there is in
PHP, Perl and sh. Either get the text from outside the script or embed
it using string literals.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.