[英]Changing URL output of User Input with JavaScript
我正在嘗試在這里構建一些簡單的東西:
用戶在輸入字段中鍵入 url,例如。 http://sharepoint.com/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com
.. 點擊“提交”,當 URL 作為鏈接吐出時,變為: https://sharepointusa.com/en-us/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com
我一直試圖吐出整個 URL,但沒有成功,丟失了參數,所以我需要一種新方法,什么是簡單的香草 javascript 只需將http://sharepoint.com/替換為https://sharepointusa.com/ en-us/並留下 URL 的 rest?
謝謝
編輯:2 個很好的答案,謝謝,我將第一個答案改編為我的原始代碼,同時我嘗試第二個答案以查看它的比較:
<a href="" id="link"></a><br>
<input type="text" id="userInput" value="Enter your text here"><br>
<input type="button" onclick="changeText2()" value="change text">
<script>
function changeText2()
{
var input=document.getElementById('userInput').value;//gets the value entered by user
const updatedUrl = input.replace('http://sharepoint.com/', 'https://sharepointusa.com/en-us/');
document.getElementById("link").href = updatedUrl;
document.getElementById("link").innerHTML = updatedUrl;
}
</script>
如果你有一個包含完整原始 url 的變量
const url = 'http://sharepoint.com/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com';
那么你可以做
const updatedUrl = url.replace('http://sharepoint.com/', 'https://sharepointusa.com/en-us/');
和 updatedUrl 將有你所要求的。
無論如何,它就在我面前得到了它。 這是如何直接從輸入字段更改它的更高級表示。
<!DOCTYPE html>
<html>
<body>
<input id="demo" value="http://sharepoint.com/human-resources/usa/Lists/testList/EditForm.aspx?ID=666&Source=http%3A%2F%sharepoint.com">
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").value;
var changed = str.replace("sharepoint", "sharepointusa");
document.getElementById("demo").value = changed;
}
</script>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.