Assign a value to JS based on the property name of the URL

Source: Internet
Author: User

1. Method One: JS Regular expression: request path: http://127.0.0.1/pec/jsp/member/refundOrder.jsp?status=4


<script>
    var status= getUrlAttribute(‘status‘);
    if(null!=status)
    {
        alert(status);
    }
    function getUrlParam(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>


2. Square Two:

Function getUrlParam(parameName){
     //location.search is a string starting from the ? of the current URL, ie the query string
     Var query = (location.search.length > 0 ? location.search.substring(1) : null);
     If(null!=query)
     {
         Var args = new Object( );
         Var pairs = query.split("&");
         For(var i = 0; i < pairs.length; i++)
         {
             Var pos = pairs[i].indexOf("=");
             If (pos == -1)
                 Continue;
             Var argname = pairs[i].substring(0,pos);
             Var value = pairs[i].substring(pos+1);
             Value = decodeURIComponent(value);
             Args[argname] = value;
         }
         / / Get the value according to the key name
         Return args[parameName];
     }
     Return null;
}

Assign a value to JS based on the property name of the URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.