jQuery 如何使用jquery从url获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12456639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:41:48  来源:igfitidea点击:

how to get data from url with jquery

jqueryjquery-mobile

提问by bdz

Is it possible to get data from an url with jquery

是否可以使用 jquery 从 url 获取数据

for example if you have www.test.com/index.html?id=1&name=boo

例如,如果你有 www.test.com/index.html?id=1&name=boo

how to get id and name?

如何获得id和name?

回答by Alessandro Minoccheri

Try this. It's pure javascript, no jQuery involved. In fact jQuery is too heavy for such a job, really.

尝试这个。它是纯 javascript,不涉及 jQuery。事实上,jQuery 对于这样的工作来说太重了,真的。

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++)
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam)
        {
            return decodeURIComponent(sParameterName[1]);
        }
    }
}?

var id = GetURLParameter('id');
var name= GetURLParameter('name');

decodeURIComponent should be used to allow parameter value contain any character, for example the very crucial equals sign =, an ampersand &or a question mark ?.

decodeURIComponent 应该用于允许参数值包含任何字符,例如非常重要的等号=、与号&或问号?

回答by Mifeng

A simple one if you know the url always has idand name

一个简单的一个,如果你知道网址总是idname

var url = "www.test.com/index.html?id=1&name=boo";
var id = /id=(\d+)/.exec(url)[1];
var name = /name=(\w+)/.exec(url)[1];

回答by gaurang171

Here i have done complete solution for above issue. please check demo link as shown below:

在这里,我已经完成了上述问题的完整解决方案。请检查演示链接,如下所示:

For that, you have first include latest jquery.js and querystring-0.9.0.js scripting files in header tag.

为此,您首先在标头标签中包含最新的 jquery.js 和 querystring-0.9.0.js 脚本文件。

Demo:http://codebins.com/bin/4ldqpac

演示:http : //codebins.com/bin/4ldqpac

HTML

HTML

<a href="#?param1=abc&param2=def">
  abc
</a>

JQuery

查询

$(function() {
    $("a").click(function() {
        setTimeout(function() {
            var param1 = $.QueryString("param1");
            var param2 = $.QueryString("param2");
            alert(param1);
            alert(param2);
        }, 300);

    });
});

Demo:http://codebins.com/bin/4ldqpac

演示:http : //codebins.com/bin/4ldqpac

回答by Ashish pathak

Try this: Working fine in IE and chrome both.Get query string values through javascript

试试这个:在 IE 和 chrome 中都可以正常工作。通过 javascript 获取查询字符串值

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

        var getData= $.urlParam('emailid');