根据输入文本生成新网址

时间:2018-03-06 15:38:35

标签: javascript

我需要根据输入文字制作新的网址

Name: <input type="text" id="myText" >
<button onclick="myFunction()">check tracking</button>

DHL将其添加到URL中,例如:
https://nolp.dhl.de/nextt-online-public/en/search?piececode=

此处还有跟踪代码。演示URL是这样的: https://nolp.dhl.de/nextt-online-public/en/search?piececode=48484848484848484

这是Javascript:

function myFunction() {
var inputed_code = document.getElementById("myText").value ;
var dhlurl = "https://nolp.dhl.de/nextt-online-public/en/search?piececode="
var new_url = dhlurl + inputed_code ;
    window.open(new_url, "_blank");
}

为什么此代码不会产生新的网址?

2 个答案:

答案 0 :(得分:0)

&#13;
&#13;
function generateUrl() {
  var dhlurl = "https://nolp.dhl.de/nextt-online-public/en/search?piececode=";
  var inputed_code = document.getElementById("myText").value;
  var new_url = dhlurl + inputed_code;
  console.log(new_url);

  //window.open wont work from the SO code snippet, but you can see url is created by looking at the console.log()
  //window.open(new_url, "_blank");
}
&#13;
Name: <input type="text" id="myText">
<button onclick="generateUrl()">check tracking</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

将javascript函数放入身体底部的脚本标签中,如下所示

<script>
function myFunction() {
var inputed_code = document.getElementById("myText").value ;
var dhlurl = "https://nolp.dhl.de/nextt-online-public/en/search?piececode="
var new_url = dhlurl + inputed_code ;
    window.open(new_url, "_blank");
}
</script>

它会起作用。