如何使用jQuery从URL获取数据 [英] how to get data from url with jquery

查看:256
本文介绍了如何使用jQuery从URL获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to get data from an url with jquery

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

如何获取ID和名称?

推荐答案

尝试一下.它是纯JavaScript,不涉及jQuery.实际上,jQuery对于这样的工作来说实在是太重了.

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

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应该用于允许参数值包含任何字符,例如非常关键的等号=,和号&或问号?.

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

这篇关于如何使用jQuery从URL获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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