How do I encode a URL in JavaScript?
I want to implement a polyvore share button into a shopping cart. Some variable values are not being passed when the script executes (description and price) and I was told that URL encoding could be a solution. Can anyone share any leads on how to apply it in JavaScript alone to my snippet? Thanks in advance.
<a href="http://www.polyvore.com/cgi/add?title=%%GLOBAL_ProductName%%&url=http://lilaboutique.co.uk/products/%%GLOBAL_ProductName%%&imgurl=%%GLOBAL_ThumbImageURL%%&desc=%%GLOBAL开发者_JAVA技巧_ProductDesc%%&price=%%GLOBAL_ProductPrice%%">
<img src="http://cdn.polyvore.com/rsrc/img/favicon.png"></a>
You can use encodeURIComponent
to encode the specific querystring values.
var url = "http://www.polyvore.com/cgi/add?title="
+ encodeURIComponent('%%GLOBAL_ProductName%%')
+ "&url=" + encodeURIComponent("http://lilaboutique.co.uk/products/"
+ encodeURIComponent('%%GLOBAL_ProductName%%')
+ "&imgurl=" + encodeURIComponent('%%GLOBAL_ThumbImageURL%%')
+ "&desc=" + encodeURIComponent('%%GLOBAL_ProductDesc%%')
+ "&price=" + encodeURIComponent('%%GLOBAL_ProductPrice%%'));
i use this library they have a lot of good things including the urlencode
See escape()
and unescape()
.
To remove "%", you can do this:
"http://www.polyvore.com/cgi/add?title=%%GLOBAL_ProductName%%&url".replace(/%/g, "")
精彩评论