Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript (trying to copy windows.location without success)

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)
like image 457
Ben Avatar asked Oct 28 '25 08:10

Ben


1 Answers

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+'';

like image 167
Diodeus - James MacFarlane Avatar answered Oct 29 '25 23:10

Diodeus - James MacFarlane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!