Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   substitute a variable with a string (http://www.velocityreviews.com/forums/t942613-substitute-a-variable-with-a-string.html)

jrough 05-24-2011 11:34 PM

substitute a variable with a string
 
I have a question why doesn't this script substitute out for the alert
line in the xmltostring function.
I don't get an alert or substitution from the a= {b}. Thanks


<html>
<body>
<script type = "text/javascript">
var b= 'Best of luck "Studentname & Roll no" for your exam';
var el = '<foo a={b}/>';
alert(el.toXMLString());
</script>
</body>
</html>

jrough 05-27-2011 12:09 AM

Re: substitute a variable with a string
 
On May 25, 1:20*am, Jake Jarvis <pig_in_sh...@yahoo.com> wrote:
Assumptions are dangerous :-)
I assumed E4X was Javascript and I could access it in my browser using
my web server.
That sounds like it was wrong. Do you mean I need a different doctype
for Javascript with XML or something like that?
If you have a web page I can read about this that would help.
I thought EcmaJS whatever was the overall standard and Javscript was
a flavor of it that pretty
much followed that standard so I don't know how you mean to implement
it or how you even know it is EcmajS
but it does make sense that I need an xml
file or something for this to work.
> On 25.05.2011 01:34, wrote jrough:
>
> > I have a question why doesn't this script substitute out for the alert
> > line in the xmltostring function.
> > I don't get an alert or substitution from the a= {b}. *Thanks

>
> Check for errors then.
>
>
>
> > <html>
> > <body>
> > <script type = "text/javascript">
> > var b= 'Best of luck "Studentname & Roll no" for your exam';
> > var el = '<foo a={b}/>';

>
> * * * * * *^^^^^^^^^^^^^^
>
> That's a string, strings don't have
> a toXMLString method.
>
> (I assume you actually are checking in an E4X supporting implementation)
>
> Use an XML initialiser
>
> | var el = <foo a={b} />;
>
> > alert(el.toXMLString());
> > </script>
> > </body>
> > </html>

>
> --
> Jake Jarvis



RobG 05-27-2011 05:43 AM

Re: substitute a variable with a string
 
On May 27, 10:09*am, jrough <janis.ro...@gmail.com> wrote:
> On May 25, 1:20*am, Jake Jarvis <pig_in_sh...@yahoo.com> wrote:
> Assumptions are dangerous :-)


Please don't top-post, reply below trimmed quotes. Don't quote
signatures or anything below them.

> I assumed E4X was Javascript and I could access it in my browser using
> my web server.


Strictly, JavaScript is a trade mark of whoever bought it from Sun
(probably Oracle I guess). It is the implementation of ECMAScript
(ECMA-262), DOM APIs etc. in Netscape and Mozilla browsers.

E4X is an extension to ECMAScript and is defined in ECMA-357. So a
JavaScript (or any other implementation of ECMAScript) could include
ECMA-357 if it wished. It has been implemented in Spidermonkey, which
is the script engine in Firefox, but not made fully available by
default (perhaps only for HTML documents).

<URL: https://developer.mozilla.org/en/SpiderMonkey >


> That sounds like it was wrong.


Perhaps.

> *Do you mean I need a different doctype
> for Javascript with XML or something like that?


It is intended to be used with XML documents, which should be valid in
regard to their XML document declaration and DOCTYPE (if included).

But you may be able to use it in HTML documents, according to the
Mozilla E4X documentation:

| In Gecko 1.8 based browsers such as Firefox 1.5,
| E4X is already partially enabled for web page
| authors. To fully enable E4X, the <script> element
| needs to have the MIME type "text/javascript;e4x=1"

<URL: https://developer.mozilla.org/en/e4x >

> If you have a web page I can read about this that would help.
> I thought EcmaJS whatever *was the overall standard and Javscript was
> a flavor of it that pretty


The language is ECMAScript, the standard is ECMA-262, the current
edition is edition 5.

<URL: http://www.ecma-international.org/pu...s/Ecma-262.htm
>


JavaScript is one implementation of ECMAScript, there are many others.
The word "javascript" or "Javascript" or "JavaScript" is often used to
generically describe ECMAScript-based scripting environments used in
web browsers and other hosts.


> much followed that standard so I don't know how you mean to implement
> it or how you even know it is EcmajS
> but it does make sense that I need an xml
> file or something for this to work.


You could start with w3schools[1] but beware: much of what you learn
there will not be strictly correct (sometimes plain wrong) but it will
get you going. More authoritative but perhaps a bit harder to get
going with is the Mozilla E4X documentation linked to above. Judging
by the similarity between the tutorial and your posted code you may
have already discovered it.

1. <URL: http://www.w3schools.com/e4x/default.asp >


--
Rob


All times are GMT. The time now is 12:40 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.