javascript - URL output of my code is not correct -
i using shopify, , current code redirect users correct url based on choice in dropdown.
i wrote code dropdown:
<ul class="clearfix filters"> <li class="clearfix filter"> <label>shop color</label> <select class="coll-filter"> <option value="/collections/all">all</option> {% col in collections %} <option value="{{ col.url }}"{% if collection.handle == col.handle %}selected="selected"{% endif %}>{{ col.title }}</option> {% endfor %} </select> </li> </ul> and script above dropdown (collection category):
<script> shopify.queryparams = {}; if (location.search.length) { (var akeyvalue, = 0, acouples = location.search.substr(1).split('&'); < acouples.length; i++) { akeyvalue = acouples[i].split('='); if (akeyvalue.length > 1) { shopify.queryparams[decodeuricomponent(akeyvalue[0])] = decodeuricomponent(akeyvalue[1]); } } } var collfilters = jquery('.coll-filter'); collfilters.change(function() { var newtags = []; var newurl = ''; collfilters.each(function() { if (jquery(this).val()) { newtags.push(jquery(this).val()); } }); {% if collection.handle %} newurl = '{{ collection.handle }}'; if (newtags.length) { newurl += '/' + newtags.join('+'); } var search = jquery.param(shopify.queryparams); if (search.length) { newurl += '?' + search; } location.href = newurl; {% else %} if (newtags.length) { shopify.queryparams.constraint = newtags.join('+'); } else { delete shopify.queryparams.constraint; } location.search = jquery.param(shopify.queryparams); {% endif %} }); </script> what code output is: http://domain.com/collections/all//collections/product
but need is: http://domain.com/collections/product
the problem should in part (location.href = newurl; ), cannot make work should.
i'm guessing fix it:
newurl = '/{{ collection.handle }}'; this makes new url root relative; without / url relative current url.
Comments
Post a Comment