Javascript function is not replacing window.location.pathname

Asked

Viewed 158 times

0

I created a function for simple search, according to the code below

function pesquisar(){
	var pesquisa = document.getElementById('pesquisa').value
	window.location.pathname = 'produtos'
	window.location.search = 'pesquisa=' + pesquisa
}
<div class="col-6 col-md-4 order-2 order-md-1 site-search-icon text-left">
  <form action="javascript:pesquisar()" class="site-block-top-search">
    <span class="icon icon-search2"></span>
    <input type="text" class="form-control border-0" placeholder="Pesquisar" id="pesquisa">
  </form>
</div>

This even works to change the search value, but the pathname value is not being changed

When the command is typed directly into the browser console, it is running perfectly

Would anyone know a possible reason?

@EDIT

If useful, below is the application backend

router.get('/produtos', function (req, res) {
    if(req.query.pesquisa){
        resultado = {nome: new RegExp(req.query.pesquisa, 'i')}
    }else{
        resultado = {}
    }
    _produto.paginate(resultado, { limit: 21, page: req.query.pag, sort: {nome: 'asc', cadastro: 'desc'} }, function (err, res) {
        if (!res.page) {
            res.page = 1
            res.nextPage = 2
            res.hasPrevPage = false
            res.hasNextPage = true
        } 
        return res
        
    }).then(function (produto) {
        res.render('produtos', { produto: produto.docs, pag: produto })
    })
})

  • Hello Jose? Where is Noode.js there? What do you want to do exactly? Because a form changes the URL via action attribute; if you use get, your variable will already be passed. If you use ajax search, you need to prevent the browser from reloading with default Prevent and collecting the data, where changing URL is just one step to this. Finally, you know Jquery?

  • Good morning, I’m still studying, I’ve done the application backend, but I don’t know good practices to perform some actions like research for example, and as I’m studying I prefer to create my own ways to do the actions, I can currently do the search by typing in the url, so I just need to change the url when the user type what they want in the input, I already edit the post and include the part of the backend

  • Remembering also, that the reason to put /products is only to redirect to the correct page at the time of the search, if the user is on another page in my application, I will keep trying, and send updates if I get something

1 answer

0


Depends on the browser you are using, there is compatibility problem, see table here:

pathname

At the time of that reply, Edge and Opera are not compatible.
Also, the pathname contains the "/" bar, and its example code does not have, need to fix this too, see the example here of the same OS:

console.log(window.location.pathname);

EDIT I also noticed in your code that you are using a javascript action on form, and action should be used to inform a url, not a Function, in which case you should use the "onsubit" event, for example:

<form class="site-block-top-search" action="#" onsubmit="pesquisar();">
  • Good morning friend, I appreciate the answer, I had already tried with / and without the bar, but the problem persisted, currently I use Chrome in version 75.0.3770.90 (Most recent until the moment of the answer), so I believe the problem is really something different

  • so it shouldn’t be a compatibility issue.. I edited the question and it includes one more detail about how you’re doing Ubmit, see if it helps

  • Before having the problem described, I had tried this way, but it seems that the action was replacing "Location.search", so I opted to remove Submit and used only the action to perform the function, if I use this way the answer I get in the url is: "? #", this with the url of the page I searched, if I search directly from the product page do I get http://localhost/products? #, since I did not succeed in my attempts I will perform the query by the application backend, using a get, I believe it is a bug in js

Browser other questions tagged

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