How do I pass a string to a function?

J

jmDesktop

I have searched much but cannot find answer to this seemingly simple
question. I have a function that builds an html element like this,
which works fine:

document.getElementById("message").innerHTML
+=
"<br>" + (i + 1) + ": <a
href='javascript:place("
+ p[1] + "," + p[0] +

")'>" + result.Placemark.address + "</
a>";

I have changed it to this:

document.getElementById("message").innerHTML
+=
"<br>" + (i + 1) + ": <a
href='javascript:place("
+ p[1] + "," + p[0] + ",'" + o.id + "','" +
result.Placemark.address +

"')'>" + result.Placemark.address + "</
a>";

There is a single quote on either side of o.id and result..., if you
cannot tell ", ' ". But this single quote never gets built. I have
tried \, escape(), String.charFromCode(39) with eval, and other
things. It just breaks the href up and what gets generated is:

<a href="javascript:place(42.415392,-71.110948,"
ctl00_contentplaceholder1_formview1_mytextbox="" ,="" 127="" main=""
st,="" medford,="" ma="" 02155,="" usa="" )="">

and this translates to:

javascript:place(42.415392,-71.110948, Why is it dropping after the
comma?

It should be:


javascript:place(42.415392,-71.110948,'ctl00_contentplaceholder1_formview1_mytextbox','127
main st, medford, ma')

It is turnning the o.id and result.Placemark.address in to
variables, and I cannot figure out why. I want to pass them as
strings to another function.

I am using Firefox 3. Thank you for any help.
 
G

GArlington

I have searched much but cannot find answer to this seemingly simple
question. I have a function that builds an html element like this,
which works fine:

document.getElementById("message").innerHTML
+=
"<br>" + (i + 1) + ": <a
href='javascript:place("
+ p[1] + "," + p[0] +

")'>" + result.Placemark.address + "</
a>";

I have changed it to this:

document.getElementById("message").innerHTML
+=
"<br>" + (i + 1) + ": <a
href='javascript:place("
+ p[1] + "," + p[0] + ",'" + o.id + "','" +
result.Placemark.address +

"')'>" + result.Placemark.address + "</
a>";

There is a single quote on either side of o.id and result..., if you
cannot tell ", ' ". But this single quote never gets built. I have
tried \, escape(), String.charFromCode(39) with eval, and other
things. It just breaks the href up and what gets generated is:

<a href="javascript:place(42.415392,-71.110948,"
ctl00_contentplaceholder1_formview1_mytextbox="" ,="" 127="" main=""
st,="" medford,="" ma="" 02155,="" usa="" )="">

and this translates to:

javascript:place(42.415392,-71.110948, Why is it dropping after the
comma?

It should be:

javascript:place(42.415392,-71.110948,'ctl00_contentplaceholder1_formview1_mytextbox','127
main st, medford, ma')

It is turnning the o.id and result.Placemark.address in to
variables, and I cannot figure out why. I want to pass them as
strings to another function.

I am using Firefox 3. Thank you for any help.


Most likely problem is to do with your "<a href='javascript:place("
Note the opening single quote in href, so when the browser encounters
next single quote it considers it to be end of href string.
You will have to escape those, or (I think this is more likely to
work) use double quotes to encapsulate your strings...
 
J

jmDesktop

I have searched much but cannot find answer to this seemingly simple
question.  I have a function that builds an html element like this,
which works fine:
                          document.getElementById("message").innerHTML
+=
                          "<br>" + (i + 1) + ": <a
href='javascript:place("
                          + p[1] + "," + p[0] +
                          ")'>" + result.Placemark.address + "</
a>";

I have changed it to this:
                          document.getElementById("message").innerHTML
+=
                          "<br>" + (i + 1) + ": <a
href='javascript:place("
                          + p[1] + "," + p[0] + ",'" + o.id + "','" +
                          result.Placemark.address +

                          "')'>" + result.Placemark.address + "</
a>";

There is a single quote on either side of o.id and result..., if you
cannot tell ", ' ".  But this single quote never gets built.  I have
tried \, escape(), String.charFromCode(39) with eval, and other
things.  It just breaks the href up and what gets generated is:
<a href="javascript:place(42.415392,-71.110948,"
ctl00_contentplaceholder1_formview1_mytextbox="" ,="" 127="" main=""
st,="" medford,="" ma="" 02155,="" usa="" )="">
and this translates to:
  javascript:place(42.415392,-71.110948,  Why is it dropping after the
comma?
It should be:
javascript:place(42.415392,-71.110948,'ctl00_contentplaceholder1_formview1_­mytextbox','127
main st, medford, ma')
It is turnning the o.id and result.Placemark.address in to
variables, and I cannot figure out why.  I want to pass them as
strings to another function.

I am using Firefox 3.  Thank you for any help.

Most likely problem is to do with your "<a href='javascript:place("
Note the opening single quote in href, so when the browser encounters
next single quote it considers it to be end of href string.
You will have to escape those, or (I think this is more likely to
work) use double quotes to encapsulate your strings...- Hide quoted text -

- Show quoted text -


I knew as soon as I posted I'd figure it out. I just use double
quotes. "\"". I didn't know you could use double quotes. I thought
that javascript would only take single quotes. I tried it and it
worked.
 
T

Thomas 'PointedEars' Lahn

jmDesktop said:
and this translates to:
javascript:place(42.415392,-71.110948, Why is it dropping after the
comma?
It should be:
javascript:place(42.415392,-71.110948,'ctl00_contentplaceholder1_formview1_­mytextbox','127
main st, medford, ma')
It is turnning the o.id and result.Placemark.address in to
variables, and I cannot figure out why. I want to pass them as
strings to another function.
I am using Firefox 3. Thank you for any help.

Most likely problem is to do with your "<a href='javascript:place("
Note the opening single quote in href, so when the browser encounters
next single quote it considers it to be end of href string.
You will have to escape those, or (I think this is more likely to
work) use double quotes to encapsulate your strings...


I knew as soon as I posted I'd figure it out. I just use double
quotes. "\"". I didn't know you could use double quotes. I thought
that javascript would only take single quotes. I tried it and it
worked.


More clearly thinking people would have simply RTFM before they started
coding in a programming language they don't know. But then again those
people would also have read the FAQ of a newsgroup before posting in it and
then would not have needed to ask this question in the first place as the
approach is wrong in the first place, and they would have properly quoted.

<http://jibbering.com/faq/>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top