How to take a parameter in a URL and assign it a new value?

Asked

Viewed 2,160 times

2

Speaking of my problem, I wanted that when passing a URL with the code below demonstrated, in the middle of the process a parameter was exchanged but specifically the parameter pagina that must go with value = 1.

// FILTROS
$('#ordem').on('change', function(){
    var url = $(this).val();
    if(url != ''){
        window.open(url, '_self');
    }   
});

The URL:

http://.../busca?&area_de=50&area_at=200&ordenacao=mxvalor&pagina=2

Within the jQuery code demonstrated, I would like each time a filter represented by ordenacao return to page 1 or simply remove the parameter and its value, which will cause my application to automatically return to page 1.

inserir a descrição da imagem aqui

  • live is depressed...

  • Have you tried to save the URL to a variable without the page parameter and only concatenate it when needed? In this case when you would have to redirect to the page without parameter you would use your variable and in the other cases you would use your URL + '?'

1 answer

3


Assuming your select has URL values and just wants to change the page value to what you get from this.vaue can do so:

$('#ordem').on('change', function(){
    var url = this.value;
    if(!url) return;
    window.open(url.replace(/pagina=[\d]+/, 'pagina=1'), '_self');
});

The new part is url.replace(/pagina=[\d]+/, 'pagina=1'). If you put your HTML I can put an example too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.