Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > javascript: passing string with quotes

javascript: passing string with quotes
Thread Tools
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status: Offline
Reply With Quote
Jan 1, 2003, 04:28 PM
 
I'm trying to pass a string to a javascript function. The problem that I am having is that the string I am trying to pass contains single quotes. I've tried escaping them, but things still aren't working quite right. I don't get the entire string, but rather the first quote and the first word of the string I am trying to pass.

Here is an example of what I am trying to pass:

Code:
javascript:rewrite('title', '<br><br>javascript:encode('string1', 'string2')')
How would I go about passing the string containing the single quotes. I tried putting in double quotes, but it doesn't seem to like that.
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Jan 1, 2003, 08:37 PM
 
javascript:rewrite("title", "java script:encode('string1', 'string2')")

That will work. If there's single quotes inside the actual strings use \' to display it as a '. If that doesn't work, supply more code so I can better view the problem. Good luck.

Travis
forté web design
     
milf  (op)
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status: Offline
Reply With Quote
Jan 2, 2003, 10:06 AM
 
The line of javascript code I need looks as follows:
Code:
{a href="javascript:showPrevious('December', '{a href="javascript:rewrite('title1', 'some lines of text')"}title1{/a}')"}
The problem that I am having is that I need to be able to include html tags and javascript code within a string parameter. The browser is reading the tags and it is screwing up my page. Is there any way to get around this?
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Jan 3, 2003, 05:08 AM
 
Yes, but it's not pretty (at least the route I came up with):

Code:
&#60;a href='javascript:showPrevious("December", "&#60;a href=javascript:rewrite(&#39;title1&#39; &#44; &#39;some lines of text&#39; )>title1&#60;/a&#62;")'&#62; the link.&#60;/a&#62;
The thing to note is in my post I am using HTML special character codes. In the code in of your page substitute the single-quotes around the month with &# 39; (minus the space) and the "inner" javascript link. If you want you can use &# 62; for the greater-than-sign and &# 60 for the less-than-sign. Make sure you don't put a space between java and script as well, IE6 doesn't recognize it that way (for me).

Travis
forté web design


//edit: grr the post keeps putting the space in the code between java and script, just make sure you're not doing it in the source code (you probably aren't..)
     
milf  (op)
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status: Offline
Reply With Quote
Jan 3, 2003, 08:29 AM
 
redjag,

That solution worked great, but there is still a problem somewhere. When I manually typed in a simple sentence for the second javascript's parameters (in the rewrite() function), everything worked how I wanted it to.

The problem now is that I am using php to pull in the contents of a file for each link. I can do that fine, but it seems as if javascript doesn't like to have certain characters in its parameters. Do you know what these characters are, so I can go about removing/replacing them? The parameter (that replaces 'some lines of text' in the rewrite() function) contains directory paths, image tags, link tags, and could possibly include other HTML tags, but those are most likely to appear. I've tried replacing <, >, ", and, ' with their respective HTML code, but I am still missing something. Do you know what other tags I need to remove? I'll try searching for a list of all characters that cause screwy javascript, so that I can hopefully prevent this from happening down the road.

Thanks.
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Jan 4, 2003, 05:21 AM
 
The only thing I can think of that you didn't mention is a line break (I think its '\n'..?). If you're taking the link from a file, how are you doing this? You may be taking the line break at the end of the line, too. Other than that, I'm not really sure. If I think of anything I'll get back to you. If you'd like to post your php and a sample file I'll take a look at it. Good luck.

Travis
forté web design
     
milf  (op)
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status: Offline
Reply With Quote
Jan 5, 2003, 04:42 PM
 
Thanks for the help guys. I actually have it working as I wanted it to.
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:00 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2