简体   繁体   中英

Redirect based on user input

I have 20 different pages. Let's say that IDs of the pages are for example: mysite.com/page1 , mysite.com/page2 and so on...

I need to create an input field where the user can enter numbers, and if he inputs number 16 for example, it will redirect him to mysite.com/page16 .

I believe that this is possible with JavaScript. Has anyone got any advice?

 window.onload = function() { document.getElementById('btn').onclick = function() { window.location.href = "http://www.yourdomain.com/page"+document.getElementById("url").value }; }
 <input id="url" value=""> <button id="btn">Go!</button>

You could use window.redirect to your desired page

var input_number = document.getElementById('input_id').value
url = "mysite.com/page" + input_number;
window.location=url;

Where input_id is the id of your input box

In vanilla JavaScript (no libraries) you can use the following script (ES6).

 let input = document.querySelector('#input'); input.addEventListener('change', event => { var url = `mysite.com/page${input.value}`; alert(url); window.location = url; });
 <input id="input" type="number" name="page" min="1" max="16" value="0"></input>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM