0

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_ProductDesc%%&price=%%GLOBAL_ProductPrice%%">
<img src="http://cdn.polyvore.com/rsrc/img/favicon.png"></a> 
bakkal
  • 54,350
  • 12
  • 131
  • 107
elramirez
  • 17
  • 3

3 Answers3

1

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%%'));
bdukes
  • 152,002
  • 23
  • 148
  • 175
  • 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%%)); var img = ""; document.write(img.link(url)); – elramirez Aug 13 '10 at 14:30
  • the above does not render the snippet. What am I doing wrong implementing your variable? THanks for the help. – elramirez Aug 13 '10 at 14:31
  • First, I'm assuming that the values in `%%` are getting replaced before we get here. You'll probably need to wrap this in quotes so that they're actually strings that you're encoding. Secondly, your method for creating the image and wrapping it in a link isn't going to work. I would suggest having the `` structure that you had before, then find the `` (via document.getElementById, jQuery selector, etc) and update its `href` attribute to the url that you create. – bdukes Aug 13 '10 at 18:06
  • Also, you'll need to make sure that the `%%` values don't have quotes in them, so that you can successfully quote them in JavaScript... – bdukes Aug 13 '10 at 18:08
  • My main limitation is that this is a served template system (bigcommerce) to which I have no access to server side scripts, so mostly have been guess work to my best knowledge trying to figure out the corresponding variables needed to add the polyvore share button. The variables that I have used render the proper content that I need for the script but now how to make it work with the share button has been another story. – elramirez Aug 13 '10 at 18:26
  • You may need to put the templated values into HTML, rather than directly into the JavaScript (since it's more likely that they're HTML encoded than JavaScript encoded). See http://jsfiddle.net/Shzxe/ for an example of that. That exmaple doesn't use a JavaScript framework, it could be cleaned up a bit if you are using a framework. It also assumes that there's only one instance of this link on the page, so it's safe to use IDs. – bdukes Aug 13 '10 at 19:51
0

i use this library they have a lot of good things including the urlencode

pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77
0

See escape() and unescape().

To remove "%", you can do this:

"http://www.polyvore.com/cgi/add?title=%%GLOBAL_ProductName%%&url".replace(/%/g, "")
Topera
  • 12,223
  • 15
  • 67
  • 104