Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

html - create a field where you put the ending of a url, click submit and it adds it to the already coded parent url

I'm trying to add a value to the end of a url that's already preset.

So for example a user would type "helloworld" into an input box, press submit and they would be taken to:

https://exxample.com/helloworld

Can I do this in HTML?

question from:https://stackoverflow.com/questions/65885096/create-a-field-where-you-put-the-ending-of-a-url-click-submit-and-it-adds-it-to

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Like this

It will go to

https://yoursite.com/portraits/

if you type portraits and hit enter

document.getElementById("urlForm").addEventListener("submit",function(e) {
  e.preventDefault()
  const val = document.getElementById("url").value.trim(); 
  const newUrl = "https://example.com/"+val+"/";
  location=newUrl;
})
<form id="urlForm">
  <input type="text" id="url" value="" />
  <input type="submit" />
</form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...