开发者

javascript (trying to copy windows.location without success)

开发者 https://www.devze.com 2023-02-20 03:26 出处:网络
This seems like I\'ve fallen down the rabbit hole and I imagine there\'s a simpler overall strategy, but here I am.

This seems like I've fallen down the rabbit hole and I imagine there's a simpler overall strategy, but here I am.

I want to allow users to sort a given set of results AFTER they've already filtered those results. I want to keep the original filters in place while adding a new one (sort). I wanted to avoid having to create hidden inputs with the original filters and then submit the form via a click event handler on the sort links.

What I tried to do instead was read the window.location and concatanate the GET parameters with my new sort parameter. It worked, except that it kept concatanating with each sort click. I only want one sort variable added per click. I tried a regex solution and it's not working. It keeps changing the windows.location variable and redirecting the page.

I'm new to js and I don't know how to deepcopy strings...or 开发者_开发百科the equivalent. How can I solve this issue? I'm also new to js regex, so pardon my naivety

   $('div#sort ul a').each(function(){
    var currentPath=window.location;
    currentPath=currentPath.replace(/sort=\w+$/,'sort='+$(this).attr('data-sort'));
    $(this).attr('href',currentPath)


window.location is not a a string, it's a "URL object". You can force it into a string like this:

var currentPath=window.location+'';

0

精彩评论

暂无评论...
验证码 换一张
取 消