在Javascript/jQuery中使用在URL中传递的变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Javascript/jQuery中使用在URL中传递的变量相关的知识,希望对你有一定的参考价值。

(I did not write this, but I forgot where I got it.)

This code will find all the variables passed though the URL and return the one you are looking for.
  1. // This function looks for all vars sent through the address bar
  2. function getUrlVars()
  3. {
  4. var vars = [], hash;
  5. var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  6. for(var i = 0; i < hashes.length; i++)
  7. {
  8. hash = hashes[i].split('=');
  9. vars.push(hash[0]);
  10. vars[hash[0]] = hash[1];
  11. }
  12. return vars;
  13. }
  14.  
  15. // This looks to see if 'sent' was a sent through the address bar and saves the value of 'sent' to a new varable called 'NewVar'
  16. var NewVar = getUrlVars()["sent"];

以上是关于在Javascript/jQuery中使用在URL中传递的变量的主要内容,如果未能解决你的问题,请参考以下文章