Portal | Homepage | Blog

Getting Rid of Quotes & Double Quotes in NitroScript


#1

Hey guys,

I have been using the available NitroScript values like {product['product_name']} by storing them as data attributes and JSON Objects. I did this so I could use these values in my jQuery scripts and AJAX requests.

I have run into a bit of a snag though… We have products that contain single quotes and double quotes in their URL, name and description. On these products (that have quotes or double quotes), my html and javascript breaks because the quotes improperly “end” attribute values and JSON values.

What I need is a way to replace these quotes before they are inserted into my values. Is there a NitroScript function available that I could use to replace single and double quotes? Something like PHP’s str_replace() or javascript’s .replace(). I can do str.replace('"', '"'), but it’s pointless because the attributes and JSON objects are broken before the javascript even runs…


#2

I need to be able to do this with NitroScript because javascript doesn’t seem to run if the page is accessed/parsed by AJAX :(.


#3

I found “Enable SEO URL normalization (Strip non-alphanumeric characters)” in the WSM’s settings, and I enabled that option. However, double quotes & single quotes remain in the URL, and the other values… I was hoping this would provide a fix.

I would set a different URL structure, but I do not wish to do that because ours are SEO friendly. So, is there anyway I can “cleanse” the NitroScript values/functions output before they are inserted into the page?

Currently, products with single quotes or double quotes break elements’ attributes that were present even before I started developing our new template. So, I think I’m not the only one with broken elements :frowning:


#4

Derek, this is true of JavaScript in general and is nothing to do with our platform.


#5

If you enable that option, you need to regenerate the URLs for it to take effect. Simply change the format and change it back to get it to do that for existing URLs. It will automatically work for new URLs.


#6

These variables aren’t designed for JSON output. You can call the adv search page and ask for JSON output, though, e.g.:

http://ws4301-4323.staging.nitrosell.com/store/advsearch.asp?submitted=1&keyword=audio+cable&output=json


#7

That’s a very handy feature! Thanks


#8

I will do so, however my issue pertains to URLs, product names, product descriptions etc.

Is this something I can pay to have created for us? We require passing the output of the NitroScript functions/variable to an element’s attributes. Doing so has allowed me to utilize NitroScript values from within my plugins and JS files.

Perhaps there is a way for me to obtain product information from within a javascript file that will allow me to avoid using HTML attributes to pass these values?

Any solution would be greatly appreciated! Especially if you have one that allows me to utilize the framework to get product data- which would render the need to pass these values via JSON/attributes unnecessary :smile:


#9

How does one regenerate their URLs by the way?


#10

You can request a specific product ID too: http://ws4301-4323.staging.nitrosell.com/store/advsearch.asp?submitted=1&product_id=75&output=json

Here’s a list of searchable fields:

product_id
product_price
product_code
product_theme
product_departmentid
product_categoryid
product_brandname
product_subcategory
product_stock
product_promotion
product_newproduct
product_specialoffer
product_keywords
product_weblinxcustomtext1
product_weblinxcustomtext2
product_weblinxcustomtext3
product_weblinxcustomtext4
product_weblinxcustomtext5
product_weblinxcustomtext6
product_weblinxcustomtext7
product_weblinxcustomtext8
product_weblinxcustomtext9
product_weblinxcustomtext10
product_subdescription1
product_subdescription2
product_subdescription3
componentdetail1
componentdetail2
componentdetail3

In a script, you could request data via AJAX and do with it what you will:

$.get("/store/advsearch.asp?submitted=1&product_id=75&output=json", function(data) {
    /* do something with data */
});

To re-generate URLs define a new format, e.g., add a new variable to the URL. You can remove it to re-generate them again afterwards if you don’t want that variable.


#11

I could kiss you right now.


#12

Seriously though, thank you. This information is priceless to me!


#13

Is it possible to return JSON data for multiple products using the code you provided?

$.get("/store/advsearch.asp?submitted=1&product_id=75,100,125,150&output=json", function(data) {
    /* do something with data */
});

The reason I ask is because I could potentially return data for multiple products with one request that way! For example, I am using AJAX to return ‘# In Stock’ currently, and I have had to conduct an AJAX request inside an each loop (for each product in the carousel)… If I could make an array of ILCs, then pass that array to the function you provided- then have that return a JSON object containing the product data for each item, I would be really happy. It would cut our page load times in half at very least!


#14

To be more specific, I want to use the function you provided in the ‘Product Panel’ to return the ‘product_stock’ of each item in the carousel. I’m doing this so the products in the carousel show how many of that item are in stock.

I saw that you can search ‘product_newproduct’, ‘product_specialoffer’ and ‘product_promotion’. But the carousel only returns a portion of the ‘Special Offer’ items. So I only want to access data for the items that are currently being displayed in the carousels…

So how can I return JSON data for a certain set of part numbers using that function?


#15

Why not just render the entire carousel using JSON data? If you specify nummatchesperpage and start parameters, you can control the number of items returned, e.g.:

http://ws4301-4323.staging.nitrosell.com/store/advsearch.asp?submitted=1&product_specialoffer=1&matchesperpage=4&start=0&output=json


#16

That would work too! lol

I can totally do that instead. I will assume that it is not possible to return data for a set of IDs, which isn’t a big deal since these carousels are the only instances where I would need to do so (pretty sure anyways).

Thank you again for the info!


#17

Only issue with going that route is that /store/advsearch.asp?submitted=1&product_specialoffer=1&matchesperpage=4&start=0&output=json will return the same 4 products every time though right?

I could probably generate a random number each time and use the ‘start=X’ variable to return different results each time… But then again, I don’t know how many results there are total, so I could potentially return no products if the random # is too big of a number… My head hurts.


#18

Okay. You can pass arrays for the integer-based parameters, like product_id. The syntax is a little complicated because you need to encode the square brackets, e.g.:

http://ws4301-4323.staging.nitrosell.com/store/advsearch.asp?submitted=1&product_id[0]=7415&product_id[1]=7797&output=json

You can see that’s passing product_id[0]=7415 and product_id[1]=7797 and it returns those two products.


#19

You are a rockstar! I liked the idea of using JSON to create the slider. I may ultimately go that route. It’s nice and clean. However, I ran into a snag with the plugin I’m using. So I may just end up using the last option you gave me to make this happen.

Regardless of which one I use, I am grateful for the insight. This will allow me to create a ton of elements quickly, which I was not able to do before. Thank you!


#20

You’re welcome Derek!