MrAdam Posted November 15, 2006 Share Posted November 15, 2006 hi does anyone know if you can send vairables in the url for javascript, like php and asp/aspx ?for example a page like: "index.html?var=blah"then somehow return the values in js?? - thanksi've found a sort-of way of doing it... using HTML domto return the value after the #"document.write(location.hash);".. will work, but.. i still would like to know if there's a way of sending multiple variables.... ? Link to comment Share on other sites More sharing options...
boen_robot Posted November 16, 2006 Share Posted November 16, 2006 One way I can think of already is if you take the location.hash as the complete string where all parameters would be and devide it by a sign, assigning each character as a member of an array. For exaple, if you had:#parameter1=value1¶meter2=value2you would need a function that would take everything after the hash as an argument and spilt it for each occurance of the & (with the split() method I guess). Then you'll need to assign each pair to an associative array. I'm not good into JS, so I can't offer you a code for that. I'm just giving you the logic to think about. Link to comment Share on other sites More sharing options...
aspnetguy Posted November 16, 2006 Share Posted November 16, 2006 This is a script I pulled of the net a while ago and it is great! /* Client-side access to querystring name=value pairs Version 1.2.3 22 Jun 2005 Adam Vandenberg*/function Querystring(qs) { // optionally pass a querystring to parse this.params = new Object() this.get=Querystring_get if (qs == null) qs=location.search.substring(1,location.search.length) if (qs.length == 0) return// Turn <plus> back to <space>// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1 qs = qs.replace(/\+/g, ' ') var args = qs.split('&') // parse out name/value pairs separated via & // split out each name=value pair for (var i=0;i<args.length;i++) { var value; var pair = args[i].split('=') var name = unescape(pair[0]) if (pair.length == 2) value = unescape(pair[1]) else value = name this.params[name] = value }}function Querystring_get(key, default_) { // This silly looking line changes UNDEFINED to NULL if (default_ == null) default_ = null; var value=this.params[key] if (value==null) value=default_; return value} to use it you do this var qs = new Querystring();var var1 = qs.get('var1'); Link to comment Share on other sites More sharing options...
boen_robot Posted November 16, 2006 Share Posted November 16, 2006 @aspnetguy this post definetly goes to my bookmarks .But one question. Does it get everything after a "?" the same way PHP does? I mean, it reads through for examplefile.html?foo=bar&god=me Link to comment Share on other sites More sharing options...
aspnetguy Posted November 16, 2006 Share Posted November 16, 2006 yup it reads it all it will returnt he value of whatever querystring variable you tell it too (as long as it is there of course) Link to comment Share on other sites More sharing options...
Andrew K. Posted November 17, 2006 Share Posted November 17, 2006 o.0Why not use location.href.split('?var=')[1]? Link to comment Share on other sites More sharing options...
Webworldx Posted November 17, 2006 Share Posted November 17, 2006 you'd need the .split('&')[0] on the end of that, but yes, I'd probably end up using something like: function get_content( vari ){ var newReg = new RegExp( vari + "=" , "i"); return location.href.split( newReg )[1].split("&")[0]}var retr = get_content("var"); Link to comment Share on other sites More sharing options...
MrAdam Posted November 17, 2006 Author Share Posted November 17, 2006 ahh great thanks! Link to comment Share on other sites More sharing options...
Webworldx Posted November 17, 2006 Share Posted November 17, 2006 Actually, if you're going to use that, i'd add an extra match statement and return false before carrying out the split just in case the variable isn't there, but apart from that. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now