获取url参数jquery或如何在js中获取查询字符串值

本文翻译自:Get url parameter jquery Or How to Get Query String Values In js

I have seen lots of jQuery examples where parameter size and name are unknown. 我已经看到许多jQuery示例,其中参数大小和名称是未知的。 My url is only going to ever have 1 string: 我的网址只会有1个字符串:

http://example.com?sent=yes

I just want to detect: 我只想检测:

  1. Does sent exist? sent是否存在?
  2. Is it equal to "yes"? 等于“是”吗?

#1楼

参考:https://stackoom.com/question/1JmaO/获取url参数jquery或如何在js中获取查询字符串值


#2楼

Try this working demo http://jsfiddle.net/xy7cX/ 试试这个工作演示 http://jsfiddle.net/xy7cX/

API: API:

This should help :) 这应该有帮助:)

code

var url = "http://myurl.com?sent=yes"

var pieces = url.split("?");
alert(pieces[1] + " ===== " + $.inArray("sent=yes", pieces));

#3楼

I hope this will help. 我希望这将有所帮助。

 <script type="text/javascript">
   function getParameters() {
     var searchString = window.location.search.substring(1),
       params = searchString.split("&"),
       hash = {};

     if (searchString == "") return {};
     for (var i = 0; i < params.length; i++) {
       var val = params[i].split("=");
       hash[unescape(val[0])] = unescape(val[1]);
     }

     return hash;
   }

    $(window).load(function() {
      var param = getParameters();
      if (typeof param.sent !== "undefined") {
        // Do something.
      }
    });
</script>

#4楼

Best solution here . 最好的解决方案在这里

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
        }
    }
};

And this is how you can use this function assuming the URL is, 假设网址是,这就是使用该功能的方式,
http://dummy.com/?technology=jquery&blog=jquerybyexample . http://dummy.com/?technology=jquery&blog=jquerybyexample

扫描二维码关注公众号,回复: 11049425 查看本文章
var tech = getUrlParameter('technology');
var blog = getUrlParameter('blog');

#5楼

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: jQuery代码片段获取作为参数存储在url中的动态变量,并将其存储为可与脚本一起使用的JavaScript变量:

$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results==null) {
       return null;
    }
    return decodeURI(results[1]) || 0;
}

example.com?param1=name&param2=&id=6 example.com?param1=name&param2=&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

#6楼

This will give you a nice object to work with 这将为您提供一个不错的对象

    function queryParameters () {
        var result = {};

        var params = window.location.search.split(/\?|\&/);

        params.forEach( function(it) {
            if (it) {
                var param = it.split("=");
                result[param[0]] = param[1];
            }
        });

        return result;
    }

And then; 接着;

    if (queryParameters().sent === 'yes') { .....
发布了0 篇原创文章 · 获赞 52 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/CHCH998/article/details/105656803