Javascript html: Constructing a Quoted Google search String
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
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
.
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>');
精彩评论