Skip to content

Instantly share code, notes, and snippets.

@andrerpena
Created March 7, 2016 00:34
Show Gist options
  • Save andrerpena/50289a7cc82aef22c7eb to your computer and use it in GitHub Desktop.
Save andrerpena/50289a7cc82aef22c7eb to your computer and use it in GitHub Desktop.
Superação
.wp-search-form {
clear: both;
padding: 20px;
background-color: #EAEAEA;
overflow: hidden;
}
.text-wrapper {
display: inline-block;
}
.search-wrapper {
margin-left: 10px;
display: inline-block;
}
function getQueryString() {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
};
function replaceUrlParam(url, paramName, paramValue){
var pattern = new RegExp('\\b('+paramName+'=).*?(&|$)')
if(url.search(pattern)>=0){
return url.replace(pattern,'$1' + paramValue + '$2');
}
return url + (url.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue
}
function searchHandler(){
var searchText = jQuery("#search-text");
var searchTerm = searchText.val();
var location = replaceUrlParam(window.location.href, 'keyword', searchTerm);
document.location = location;
};
jQuery(function() {
var queryString = getQueryString();
console.log(queryString);
jQuery('.sell-media-breadcrumbs').after('<div class="wp-search-form"></div>');
var searchForm = jQuery(".wp-search-form");
searchForm.append('<div class="text-wrapper"><input type="text" id="search-text"/></div>');
var searchText = jQuery("#search-text");
searchText.on('keypress', function(event) {
if(event.which == 13) {
searchHandler();
}
});
searchText.val(queryString['keyword']);
searchForm.append('<div class="search-wrapper"><button id="search-btn">Pesquisar</button></div>');
var searchBtn = jQuery("#search-btn");
searchBtn.click(searchHandler);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment