In web development, sometimes we need to pass some data between different web pages. At this time, we can use HTML to pass values. HTML value transfer is a common method, but some problems may also be encountered during use, such as garbled characters.
Common ways to pass values in HTML include URL parameter passing, form submission, cookie and session, among which URL parameter passing and form submission are the most commonly used methods. URL parameter passing is to transfer data between different pages through URL. For example, we can use the following code to pass a parameter in a page:
<a href="target.html?name=张三&age=20">传递参数</a>
In the target page, the parameter value can be obtained through JavaScript:
<script> var name = getQueryString("name"); var age = getQueryString("age"); function getQueryString(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if(r!=null)return unescape(r[2]); return null; } </script>
In the above code, we use JavaScript Use the regular expression method to obtain parameter values, and use the unescape() method to decode garbled characters.
However, in actual development, we may encounter some unresolved garbled code problems. At this time, we need to think about solutions.
First, we can try to encode the parameter value to avoid garbled characters. There are two common encoding methods, namely URL encoding and Base64 encoding.
URL encoding converts all special characters into %xx format encoding, where "%xx" represents a string of hexadecimal representation of the character's ASCII code value. For example, we can use JavaScript's encodeURIComponent() method to URL-encode the parameters:
<a href="target.html?name=<%=encodeURIComponent('张三')%>&age=<%=encodeURIComponent('20')%>">传递参数</a>
In the target page, we can use the decodeURIComponent() method to decode:
<script> var name = decodeURIComponent(getQueryString("name")); var age = decodeURIComponent(getQueryString("age")); function getQueryString(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if(r!=null)return unescape(r[2]); return null; } </script>
Base64 encoding is about data. Base64 conversion to avoid garbled characters. For example, we can use JavaScript's btoa() method to Base64 encode the parameters:
<a href="target.html?name=<%=btoa('张三')%>&age=<%=btoa('20')%>">传递参数</a>
In the target page, we can use the atob() method to decode:
<script> var name = atob(getQueryString("name")); var age = atob(getQueryString("age")); function getQueryString(name){ var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if(r!=null)return unescape(r[2]); return null; } </script>
In addition to encoding, we also You can use meta tags to specify the encoding method of the page, for example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
The above are some methods to solve the problem of garbled HTML value transmission. You need to choose the appropriate method according to the actual situation. At the same time, we also need to pay attention to protecting user privacy and avoid passing sensitive information through URLs.
The above is the detailed content of html html passed value garbled code. For more information, please follow other related articles on the PHP Chinese website!