0

I would like to construct a quoted google search string, and put this string in a link. The user would click on the link and be taken to the quoted search result. (Using JQuery)

$(".abstract_text",this).append('<div><a href="http://google.com/search?btnI=1&q="'+art_title.make_search()+'"> Search For Fulltext</a></div>');

The above code gives this output:

http://google.com/search?btnI=1&q=The+Q+switched+ND+YAG+laser+effectively+treats+tattoos+in+darkly+pigmented+skin

I would like to find a way to produce this output:

http://google.com/search?btnI=1&q="The+Q+switched+ND+YAG+laser+effectively+treats+tattoos+in+darkly+pigmented+skin"

My question seems to be related to this question but I'm not exactly sure how to apply the answer that was accepted there: Nesting Quotes in JavaScript/HTML

*Please Note that though the search results are the same regardless of the output in this case, they will be different in general.

Solution:Use the encode URI function

$(".abstract_text",this).append('<div><a href="http://google.com/search?btnI=1&q='+encodeURI('"'+art_title.make_search()+'"')+'"> Search For Fulltext</a></div>');

Output:

http://www.google.com/search?btnI=1&q=%22Early+treatment+of+traumatic+tattoo+by+erbium+YAG+laser%22
Community
  • 1
  • 1
tjb
  • 11,480
  • 9
  • 70
  • 91
  • Add a jQuery tag if you're using jQuery. Otherwise, people will think this is a pure JS question. –  Jan 18 '11 at 19:04
  • Ok Matt, though in the end it did turn out to be a general problem not specifically related to jQuery – tjb Jan 18 '11 at 20:06

2 Answers2

0

Try this:

$(".abstract_text", this).append('<div><a href=\'http://google.com/search?btnI=1&q=\"'+art_title.make_search()+'\"\'> Search For Fulltext</a></div>');
Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
  • Doesn't work. The output is this: http://google.com/search?btnI=1&q= ...then nothing more. When I inspect the element with firebug the rest of the text has been turned into an attribute. Search For Fulltext hmm ... intresting. – tjb Jan 18 '11 at 19:55
  • That is interesting.. Tried it on Chrome and it seems to work fine :P Ah well, encoding really is the correct way to go. – Demian Brecht Jan 18 '11 at 20:06
  • I was using firefox, just another obscure difference between the two I guess. – tjb Jan 18 '11 at 20:34
0

I don't know about JQuery but I guess what you want to do is encode your URL's special chars. You can use the native Javascript function encodeURI() (function reference)

The default encoding for double quotes " is %22.

Gabriel S.
  • 1,961
  • 2
  • 20
  • 30