1

Basically I have a url that needs to be changed dinamically to call on another functions, so for example I have "index/buscasimples/finalidade/1/filtro/1/", and when a specific button is pressed, I need to replace the part where it is "filtro/1/" for another one. The problem is that this number "/1/" can vary, from 1 to 3, so I'll probably need an if to do the work. But I want to know if there is another way that could help me...

To be clearer, the code is like this

  $('#filtro').change(function(e) { 
    e.preventDefault();
    var filtro = $('#filtro option:selected').val();
    var urlAtual = window.location.href; 
    var url = urlAtual.replace('/buscasimples/','/buscafiltrada/');
    url += 'filtro/'+filtro+'/';
    window.location = url;
})

This function changes the url and put on the "filtro" that is needed, and work fine for the first time. But then when the proccess is repeated, there'll be 2 "filtro" in the url, so I want to replace the "filtro" and the number that is next to it, but this number can be 1, 2 or 3 as I said. There's some native jquery function that can replace one character whatever they are like "replace.('filtro/@/')" or something? Sorry for long question, I got that application already made and I'm trying to deal with it haha Thanks!!

2
  • Where's the option html code ? Commented May 14, 2016 at 1:16
  • ` <select name="filtro" id="filtro" class="sel-custom form-control"> <option value="" selected disabled>Escolha um filtro</option> <option value="1">Nenhum</option> <option value="2">Maior preço</option> <option value="3">Menor preço</option> </select>` Commented May 16, 2016 at 12:10

2 Answers 2

3

I think you need indexOf() .. try this code and read the comments in the code please

$('#filtro').change(function(e) { 
    e.preventDefault();
    var filtro = $('#filtro option:selected').val();
    var urlAtual = window.location.href; 
    var url = urlAtual.replace('/buscasimples/','/buscafiltrada/');
    // <<<<<<<<< the new code start 
    if(url.indexOf('filtro/') > -1){  // if filtro/ found in the url'
       urlSplit = url.split('filtro/'); // split url to get the url before 'filtro/'
       url = urlsplit[0]+'filtro/'+filtro+'/'; // append new filtro to the first url string
    }else{  // if url doesn't have filtro then append to url
       url += 'filtro/'+filtro+'/'; 
    }
    // <<<<<<<<<<< end of new code
    window.location = url;
})
Sign up to request clarification or add additional context in comments.

2 Comments

Man that was genius work! I'm really glad to find people who spend time like this to help others, you really made my day better! All the best, thankss!
@op_exchanger l'm really glad to make your day better.. and hope all your days comes better and better .. keep awesome and enjoy coding ... good luck :)
1

You can check if the current url already contains filtro/number, is so, we replace it with the new filtro number, otherwise, we add filtro/numero to the url and redirect.

You can use something like:

$('#filtro').change(function(e) { 
var filtro = $('#filtro option:selected').val();
var urlAtual = window.location.href; 
var url = urlAtual.replace('/buscasimples/','/buscafiltrada/');
if (/filtro\/\d+/i.test(url)) {
    url = url.replace(/filtro\/\d+/i, "filtro/"+filtro);
}else{
    url += 'filtro/'+filtro+'/';
}
window.location = url;
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.