I have a Google Instant style search script written in jQuery. When the user queries, #search/SEARCHTERM/1/ is added onto my page URL. How can I make it so that my URL is without the # at the start?
Here is my current jQuery code:
$(document).ready(function(){
$("#search").keyup(function(){
var search=$(this).val();
var query=encodeURIComponent(search);
var yt_url='search.php?q='+query+'&category=web';
window.location.hash='search/'+query+'/1/';
document.title=$(this).val()+" - My Search Script";
if(search==''){
window.location.hash='';
document.title='My Search Script';
}
$.ajax({
type:"GET",
url:yt_url,
dataType:"html",
success:function(response){
$("#result").html(response);
}
});
});
});
You cannot remove the hash from hash 🙂 but you can alter the whole url With the pushState() method.
https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState().c2.a0method
Note that this won’t work in older browsers and you have to have content behind every url you generate with JS.