我基本上是在尝试使用用户输入创建一个对 url 友好的查询字符串。变量替换空格和@s 并将它们加在一起,然后函数应该在段落中显示url。这让我觉得自己很愚蠢,因为我知道这很简单,我搞砸了。请指出我的错误。附带说明一下,是否有更好的方法可以在没有所有变量的情况下进行所有这些替换?
以下代码应返回类似于以下内容的字符串: https ://www.somewebsite.com/formhandler.php?&fname=gordie&address=123+main+street&email=something%40somewhere.com
<SCRIPT TYPE="TEXT/JAVASCRIPT">
var fnstr = document.getElementById("firstname").value;
var fncutspace = fnstr.trim();
var fnrepspace = fncutspace.replace(/ /g,"+");
var fnfinal = fnrepspace.replace("@","%40");
var adstr = document.getElementById("address").value;
var adcutspace = adstr.trim();
var adrepspace = adcutspace.replace(/ /g,"+");
var adfinal = adrepspace.replace("@","%40");
var emstr = emdocument.getElementById("firstname").value;
var emcutspace = emstr.trim();
var emrepspace = emcutspace.replace(/ /g,"+");
var emfinal = emrepspace.replace("@","%40");
var domurl = "https://www.somewebsite.com/formhandler.php?&fname=";
var secondchunk = "&address=";
var thirdchunk = "&email=";
var url = domurl + fnfinal + secondchunk + adfinal + thirdchunk + emfinal;
function createurl() {
document.getElementById("demo").innerHTML = url;
}
</SCRIPT>
<FORM action="javascript:createurl()">
Fisrt Name:<INPUT id="firstname" TYPE="TEXT"><br>
street address:<INPUT id="address" TYPE="TEXT"><br>
email address:<INPUT id="email" TYPE="TEXT"><br>
<input type="submit" value="GO">
</FORM>
<p id="demo"></p>