![]() |
Just curious - how to do: Name JS variables the same as URL parameters
Hello, everyone.
I'm looking for a way to take URL parameters (via document.URL) and name JavaScript variables the same as the URL parameters. For example: Let's say the URL is http://www.domain.com/index.html?a=1...ething&c=carpe diem. I've got code that will split on "?", then split on "&", then split on "=", so I've got an array. How can I now loop through that array, and set var array[0] = array[1], and get JavaScript variables a, b, and c with their associated value? Thank you, PS: I should add - WITHOUT using eval. eval() is evil. |
Maybe it will help you if you realize that every variable name in the global scope is automatically the key of an associative array called "window" if you use it as a string
i.e. Code:
var myVar = 5;Code:
window[array[0]] = array[1]; |
Code:
<script> |
Code:
<script type="text/javascript">Code:
<script type="text/javascript">"Education is the process of casting imitation pearls before real swine" - Irwin Edman |
code
Code:
// turn URL'QS into an object using a parser. takes full urls...Code:
alert(shows: Code:
{ |
Awesome! Thank you, guys, for the input. I'm going to give each of them a shot and report back.
|
All suggestions were excellent, but devnull69's sample was very simple and nailed it.
Thanks, again, everyone. |
| All times are GMT +1. The time now is 07:09 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.