简体   繁体   English

JavaScript HTML转义按钮

[英]JavaScript HTML escape button

I am trying to make a button which would encode my link into ascii and than on click redirect me to that new encoded address. 我正在尝试制作一个将链接编码为ASCII的按钮,然后单击将我重定向到该新的编码地址。

I already have the encoding part but the button just displays the converted code on click. 我已经有了编码部分,但是该按钮仅在单击时显示转换后的代码。 I would like it instead to redirect me to that encoded page. 我想改为将我重定向到该编码页面。

<body>

<p>Click the button to encode a URI.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var uri = "https://webpage.com/submit.do?firstName=Ždžrgr&lastName=Žrđredj&phone=(24324) 09942&email=email@email.net”;
    var res = encodeURI(uri);
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>

Can someone please help me with this? 有人可以帮我吗? I am new to JavaScript. 我是JavaScript新手。

You're setting the innerHTML instead of changing the location. 您在设置innerHTML而不是更改位置。 Try using 尝试使用

this.document.location = res;

instead. 代替。

You can use location.href to redirect the page. 您可以使用location.href来重定向页面。 getElementById() gets the element with id inside the paranthesis from the page for getting or setting the current value. getElementById()从页面获取括号中具有id的元素,以获取或设置当前值。

function myFunction() {
    var uri = "https://webpage.com/submit.do?firstName=Ždžrgr&lastName=Žrđredj&phone=(24324) 09942&email=email@email.net";
    var res = encodeURI(uri);
    location.href = res;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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