Get escaped URL parameter

后端 未结 19 2679
小蘑菇
小蘑菇 2020-11-21 13:41

I\'m looking for a jQuery plugin that can get URL parameters, and support this search string without outputting the JavaScript error: \"malformed URI sequence\". If there is

19条回答
  •  抹茶落季
    2020-11-21 13:54

    jQuery code snippet to get the dynamic variables stored in the url as parameters and store them as JavaScript variables ready for use with your scripts:

    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if (results==null){
           return null;
        }
        else{
           return results[1] || 0;
        }
    }
    
    example.com?param1=name¶m2=&id=6
    
    $.urlParam('param1'); // name
    $.urlParam('id');        // 6
    $.urlParam('param2');   // null
    
    //example params with spaces
    http://www.jquery4u.com?city=Gold Coast
    console.log($.urlParam('city'));  
    //output: Gold%20Coast
    
    console.log(decodeURIComponent($.urlParam('city'))); 
    //output: Gold Coast
    

提交回复
热议问题