获取url参数jquery或如何在js中获取查询字符串值 [英] Get url parameter jquery Or How to Get Query String Values In js

查看:92
本文介绍了获取url参数jquery或如何在js中获取查询字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到许多jQuery示例,其中参数大小和名称是未知的.我的网址只会有1个字符串:

I have seen lots of jQuery examples where parameter size and name are unknown. My url is only going to ever have 1 string:

http://example.com?sent=yes

我只想检测:

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

推荐答案

最佳解决方案此处.

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]);
        }
    }
};

这是假设网址为
时如何使用此功能的方法 http://dummy.com/?technology=jquery&blog=jquerybyexample.

And this is how you can use this function assuming the URL is,
http://dummy.com/?technology=jquery&blog=jquerybyexample.

var tech = getUrlParameter('technology');
var blog = getUrlParameter('blog');

这篇关于获取url参数jquery或如何在js中获取查询字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆