zoukankan      html  css  js  c++  java
  • JS获取url参数及url编码、解码

    完整的URL由这几个部分构成:scheme://host:port/path?query#fragment ,各部分的取法如下:

    window.location.href:获取完整url的方法:,即scheme://host:port/path?query#fragment

    window.location.protocol:获取rul协议scheme

    window.location.host:获取host

    window.location.port:获取端口号

    window.location.pathname:获取url路径

    window.location.search:获取参数query部分,注意此处返回的是?query

    window.location.hash:获取锚点,#fragment

    在js中可以使用escape(), encodeURL(), encodeURIComponent(),三种方法都有一些不会被编码的符号:

    escape():@ * / +

    encodeURL():! @ # $& * ( ) = : / ; ? + '

    encodeURIComponent():! * ( ) '

    在java端可以使用URLDecoder.decode(“中文”, "UTF-8");来进行解码

    但是由于使用request.getParameter()来获取参数时已经对编码进行了一次解码,所以一般情况下只要在js中使用

    encodeURIComponent("中文");

    在java端直接使用request.getParameter()来获取即可返回中文。

    如果你想在java端使用URLDecoder.decode(“中文”, "UTF-8");来解码也可以在js中进行二次编码,即:

    encodeURIComponent(encodeURIComponent("中文"));

    如果不进行二次编码的话,在java端通过decode方法取的会是乱码。

  • 相关阅读:
    348. Design Tic-Tac-Toe
    347. Top K Frequent Elements
    346. Moving Average from Data Stream
    345. Reverse Vowels of a String
    343. Integer Break
    342. Power of Four
    341. Flatten Nested List Iterator
    340. Longest Substring with At Most K Distinct Characters
    339. Nested List Weight Sum
    Python(九) Python的高级语法与用法
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/4809576.html
Copyright © 2011-2022 走看看