wikiやその他のサイトでユーザー入力が検索されるようにしようとしています。プロンプトでユーザーの入力を使ってwiki上で何かを検索する方法
var input = prompt();
if(input === "") {
window.location.href = ("https://en.wikipedia.org/wiki/Steve_Jobs");
};
考えられますか?
wikiやその他のサイトでユーザー入力が検索されるようにしようとしています。プロンプトでユーザーの入力を使ってwiki上で何かを検索する方法
var input = prompt();
if(input === "") {
window.location.href = ("https://en.wikipedia.org/wiki/Steve_Jobs");
};
考えられますか?
// Store the user input in a variable
var input = prompt();
// If the input wasn't empty, continue
if (input !== "") {
// Send the user to the URL concatenating the input onto the end
window.location.href = ("https://en.wikipedia.org/wiki/" + input);
}
あなたは、ユーザーの入力を取り、アンダースコアとスペースをスワップアウトし、その後、クエリの最後にそれを平手打ちすることができます
var input = prompt();
// Replace any spaces with underscores and remove any trailing spaces
input = input.trim().split(' ').join('_');
// If the user gave some input, let's search
if(input.length) {
window.location.href = ("https://en.wikipedia.org/wiki/" + input);
};
私はあなたがウィキペディアを検索したい場合、あなたは検索用語を追加することができたとしウィキペディアの検索URLにクエリ文字列パラメータとして下図のように:
// Prompt the user for something to search Wikipedia for
var input = prompt();
// If you actually have something, then search for it
if(input.trim().length > 0){
// Replace any spaces with + characters and search
window.location.href = 'https://en.wikipedia.org/w/index.php?search=' + input.replace(' ','+');
}
ワーキングスニペットを
var input = prompt('What do you want to search Wikipedia for?');
if(input.trim().length > 0){
/// Replace any spaces with + characters and search
window.location.href = 'https://en.wikipedia.org/w/index.php?search=' + input.replace(' ','+');
}
ウィキまたは別のサイトを検索するには、サイトのURL構造を熟知している必要があります。たとえば、Wikipediaで「https://en.wikipedia.org/w/index.php?search=user+input」の形式を使用して検索を開始することができます
Nick Zuberと同様のコードを使用してこれを行うことができます。
var input = prompt();
// Replace any spaces with pluses
input = input.split(' ').join('+');
// If the user gave some input, let's search
if(input.length) {
window.location.href = ("https://en.wikipedia.org/w/index.php?search=" + input);
};