如何在jquery ready函数中获取URL参数? [英] How can I get URL parameter in a jquery ready function?

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

问题描述

我有以下内容:

$(document).ready(function() {
window.location.href='http://target.SchoolID/set?text=';
});

因此,如果有人使用以下网址访问包含上述代码的网页:

So if someone comes to a page with the above mentioned code using a url like:

Somepage.php?id = abc123

Somepage.php?id=abc123

我希望ready函数中的text变量读取:abc123

I want the text variable in the ready function to read: abc123

任何想法?

推荐答案

你不需要jQuery。你可以用普通JS做到这一点

you don't need jQuery. you can do this with plain JS

function getParameterByName( name ) //courtesy Artem
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

然后你就可以得到这样的查询字符串值:

and then you can just get the querystring value like this:

alert(getParameterByName('text'));

在这里查看其他方法和插件:如何在JavaScript中获取查询字符串值?

also look here for other ways and plugins: How can I get query string values in JavaScript?

这篇关于如何在jquery ready函数中获取URL参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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