Skip listing all examples if query param is set.
Also use the `input` event instead of `keyup` and addEventlistener instead of setting the onload / onchange properties. And handle space character encoded as `+` in query string correctly.
This commit is contained in:
@@ -102,30 +102,32 @@
|
||||
listExamples(examples);
|
||||
}
|
||||
|
||||
function parseQuery() {
|
||||
function parseParams() {
|
||||
const params = {};
|
||||
const list = window.location.search.substring(1).split('&');
|
||||
const list = window.location.search
|
||||
.substring(1)
|
||||
.replace(/\+/g, '%20')
|
||||
.split('&');
|
||||
for (let i = 0; i < list.length; ++i) {
|
||||
const pair = list[i].split('=');
|
||||
if (pair.length == 2) {
|
||||
if (pair.length === 2) {
|
||||
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
|
||||
}
|
||||
}
|
||||
if (params['q']) {
|
||||
const input = document.getElementById('keywords');
|
||||
input.value = params['q'];
|
||||
inputChange.call(input);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
window.onload = function () {
|
||||
window.addEventListener('load', function () {
|
||||
for (let i = 0; i < info.examples.length; ++i) {
|
||||
info.examples[i].link += window.location.search;
|
||||
}
|
||||
template = new jugl.Template('template');
|
||||
target = document.getElementById('examples');
|
||||
listExamples(info.examples);
|
||||
document.getElementById('keywords').onkeyup = inputChange;
|
||||
parseQuery();
|
||||
};
|
||||
const params = parseParams();
|
||||
const text = params['q'] || '';
|
||||
const input = document.getElementById('keywords');
|
||||
input.addEventListener('input', inputChange);
|
||||
input.value = text;
|
||||
filterList(text);
|
||||
});
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user