当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - History pushState() stacking up

问题描述:

I'm having a problem on my page. I've set up a process, so that every time you tick a search term box, the page loads contents on those terms and also changes the url according to the terms.

The problem right now is, that I can't find a way to make only a part of the url change.

The code is as follows:

 $('input[name*=malli]').change(function(e) {

// Code that loads the content

data = searchterm;

history.pushState(null, null, data);

So what this does, is make the url from

http://localhost:8000/fi/search_test

to

http://localhost:8000/fi/5

I've tried adding the search_test into the pushState also, but this leads into going from

http://localhost:8000/fi/search_test

to

http://localhost:8000/fi/search_test/5

into

http://localhost:8000/fi/search_test/5/search_test/5-8

Any help is appriciated

网友答案:

I would be better to catch pathname (ie. "/fi/search_test/" with ending slash !) with :

var search_text_path = window.location.pathname || "/fi/search_test/";

Then

 $( 'input[name*=malli]' ).change( function() {
     data = search_text_path + encodeURIComponent( this.value );
     history.pushState( null, null, data );
 });
分享给朋友:
您可能感兴趣的文章:
随机阅读: