JavaScript - Array To Query String
Hello everybody !
I'm searching for a javascript function that convert array to query string. Like this example : Code: var array = { a=1, b=2, c=3 }; to a=1&b=2&c=3 I'm using jQuery and the function .serialize() convert only html forms. Does someone know a function to do this ? Thanks ! Similar TutorialsHey all, I have an issue building a query string. Basically, I have this: /sites?order=site_numper_page=20& But I would like to convert it to this: /sites?order=site_num&per_page=20 I have tried this to check if the current iterator is less than the length of the object and if so, then add the "&", otherwise, don't. But it doesn't seem to work: Code: $.each(query_string, function(key,value){ params += key + '=' + value; if((value + 1) != query_string.length){ params += "&"; } }) Thanks for any response. Hello all, essentially i am grabbing a variable from the query string and i need to pass that value into another script. allow me to explain; usually i use this code to display a persons email address from the query string onto the page using document.write <script> function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); if (pair[0] == variable) { return pair[1]; } } document.write(''); } </script> then i would display the var with this <script> document.write( getQueryVariable("Contact0Email") ); </script> ------------ ok so here is what i need to do. my client sent me the code below asking me to pass the email var into the javascript and image. my programmer friend said to use php and do smething like; <SCRIPT language="javascript" src="https://www.cpctrack.com/redirconsume.aspx?CID=25240&ORDERID=<? echo . $_GET['Contact0Email']; ?>"></SCRIPT> <NOSCRIPT><IMG src="https://www.cpctrack.com/redirconsume.aspx?CID=25240&p=img&ORDERID=<? echo . $_GET['Contact0Email']; ?>" width="1" height="1" border="0"></NOSCRIPT> this syntax does not work; <? echo . $_GET['Contact0Email']; ?> WHAT DO YOU GUYS THINK? Is the syntax wrong or are there better methods? Hi I am trying to create a query string similar to the one available he http://andrewu.co.uk/tools/request/e.../example1.html The problem that I am having is that I have more than one button on my page and even though my id's are unique all of my data is displaying on the action page at the same time. The code that i am using for my buttons is: Code: <label for="name_1">Name:</label> <input type="text" name="name1" id="name_1" tabindex="1" size="40" value="Donald Duck" /> <br /> <br /> <input type="submit" id='view1' value="Submit" tabindex="2" /> <label for="name_1">Name:</label> <input type="text" name="name2" id="name_2" tabindex="1" size="40" value="Mickey Mouse" /> <br /> <br /> <input type="submit" id='view2' value="View" tabindex="2" /> I have a lot more than 2 buttons on my page. when a button is clicked I would like to be able to get the values from the query string and only see the values for the button that was clicked like this one: http://andrewu.co.uk/tools/request/e...ndrew+Urquhart but instead I am getting Name: Donald Duck, Name: Mickey Mouse. What would I have to add or change to obtain just one Name to display at a time but have it be different depending on which button is pressed? Hello Friends... I am working on parsing the querystring using javascript So I have successfully parsed the querystring & also replace the special character+ but I want to display the querystring in the TextField Control So how to implement it Please guide me.... Hey I need help to obtain the requested javascript's query string. Example: Code: <script type="text/javascript" src="script.js?v=123"></script> How can I use that query string inside script.js for example like Code: var v = 'The code to get the query fragment'; alert(v); // alerts '123' Thank you. - DJCMBear Hi all, I would really appreciate it if someone could help me with this problem. I have some JavaScript code that encodes (escapes) a string (typically a URL) - I then want to pass the encoded string to a php page via a querystring. Trouble is, the querystring doesn't contain the correct encoded string! For example - If we assume the string I want to pass contains the data : tester My encoder, encodes this as : %74%65%73%74%65%72 When the code then passes this in a query string to, for example, code.php the querystring comes out as follows : code.php?encstring=tester The following is the JavaScript code that encodes, shows an alert box with the correct encoded string in it and then calls the code.php with the encoded string attached. Please help? What am I missing here? Code: NOTE: variables: os is the input string and ns is the encoded string. Code: <script type="text/javascript"> var os='tester'; var ns=''; var t; var chr=''; var cc=''; var tn=''; for(i=0;i<256;i++){ tn=i.toString(16); if(tn.length<2)tn="0"+tn; cc+=tn; chr+=unescape('%'+tn); } cc=cc.toUpperCase(); os.replace(String.fromCharCode(13)+'',"%13"); for(q=0;q<os.length;q++){ t=os.substr(q,1); for(i=0;i<chr.length;i++){ if(t==chr.substr(i,1)){ t=t.replace(chr.substr(i,1),"%"+cc.substr(i*2,2)); i=chr.length; }} ns+=t; } alert(ns); window.location = "code.php?encstring="+ns; </script> Code: <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script> function getQuerystringNameValue(subid) { var winURL = window.location.href; var queryStringArray = winURL.split("?"); var queryStringParamArray = queryStringArray[1].split("&"); var nameValue = null; for ( var i=0; i<queryStringParamArray.length; i++ ) { queryStringNameValueArray = queryStringParamArray[i].split("="); if ( subid == queryStringNameValueArray[0] ) { nameValue = queryStringNameValueArray[1]; } } return nameValue; } </script> <script> function getQuerystringNameValue(uid) { var winURL = window.location.href; var queryStringArray = winURL.split("?"); var queryStringParamArray = queryStringArray[1].split("&"); var nameValue = null; for ( var i=0; i<queryStringParamArray.length; i++ ) { queryStringNameValueArray = queryStringParamArray[i].split("="); if ( uid == queryStringNameValueArray[0] ) { nameValue = queryStringNameValueArray[1]; } } return nameValue; } </head> And trying to pass into this link but its not working. Code: <map name="Map"> <area id="link-connector" shape="rect" coords="33,466,349,530" href="http://www.linkconnector.com/ta.php?lc=125601055059003882&atid=" + getQuerystringNameValue("uid") + "&ptid=" + getQuerystringNameValue("subid") + "&lcpt=0&lcpf=3"> </map> <body> Reply With Quote 01-13-2015, 07:21 PM #2 rnd me View Profile View Forum Posts Visit Homepage Senior Coder Join Date Jun 2007 Location Urbana Posts 4,497 Thanks 11 Thanked 603 Times in 583 Posts you can't use script in the middle of an attrib like that, you need to hard-code that url in whole, or build the string in JS and apply it all at once. The code is JScript .NET, which is basically a .NET version of Javascript. Regardless of language, Anyone with appending type of skill can answer my question. The problem is, when I keep issuing the request it keeps appending over and over again Code: if (oSession.uriContains("search?q=")) { var str = oSession.fullUrl; var sAppend = "+test1+test2+test3"; if (!oSession.uriContains(sAppend)) { oSession.fullUrl = str.replace( "search?q=","search?q="+sAppend); } } Can you please give me an example? I'm still new to programming. Thank you Hello, I am trying to update an iframe when the user makes a selection from a select box. I don't have any trouble with changing the iframe, but when I try to send a varible as a quey string, the variable doesn't pass to the iframe properly. // this is in my header, and it is initiated from the onchange below. // it changes the page fine, but doesn't read the varible "num" // that was passed to the function <script language="javaScript"> <!-- function updatenotes (num) { document.getElementById("desc").src = 'iframe1.cfm?dec=num' } //--> </script> // this is the select box that initiates the function from onchange // for simplicities sake, I hard coded 3 options, but in reality, // these options are ever changing <select name="Description" onchange="updatenotes(this.options[selectedIndex].value)"> <option name="">Select a project</option> <option name="2" >project2</option> <option name="3" >project3</option> </select> Any help you can offer would me ver appreciated. thanks Hey all, I'm in process of writing my own script (I don't want to use any jquery plugins) to render a table and allow for filtering/searching. So when user clicks on link, by default there will be parameters passed in query string as you can see in the setDefaults private method below. However, I'm stuck trying to figure out the best way to append values selected from filter dropdown to the query string. I know it will involve something along the lines of $("select").change(function(){var option = $(this).val()}). But I am having difficulty figuring out how to incorporate it with this. I would like to make use of the below buildFilter function somehow: Code: (function($){ var listview = $('#listview'); var lists = (function(){ var criteria = { drop_filter: { check: function(value){ if(value) return }, msg: "Loading..." }, search_filter: { check: function(value){ if(value) return }, msg: "Loading..." } } var handleFilter = function(){ } return { create: function(component){ var component = component.href.substring(component.href.lastIndexOf('#') + 1); return component; }, setDefaults: function(component){ var parameter = {}; switch(component){ case "sites": parameter = { 'order': 'site_num', 'per_page': '20', 'url': '/sites' } } return parameter; }, buildFilter: function(){ if(event == "change") return criteria.drop_filter(); } } })(); $('#dashboard a').click(function(){ var currentComponent = lists.create(this); var custom = lists.setDefaults(currentComponent); initializeTable(custom); }); var initializeTable = function(custom){ var defaults = {}; var query_string = $.extend(defaults, custom); var params = []; $.each(query_string, function(key,value){ params += key + ': ' + value; }) alert(params); } })(jQuery) Thanks for any response. I have an array: Code: var array_123=["test1","test2","test3"]; I have a string: Code: var array_string = "array_" + "123"; I would like to use the value of array_string (array_123) as an array to parse out the values of it. But this isn't working: Code: for (var x in array_string) { var test_ary = array_string[x]; } Any help? So i've written up the code to do a lightbox esque overlay image gallery. everything is working smooth and fine. The only concern is that I have the following: lets say that I have a page that has multiple galleries. Obviously I would have to make each literal array with the gallery data different names, ex: Code: var _gallery1 = { 'images': [ { 'id' : 1, 'src' : "" } ] } var _gallery2 = { 'images': [ { 'id' : 1, 'src' : "" } ] } or would it be easier to do it multi dimensional? Code: var gallery_data = { 'gallery1': [ 'images': [ { 'id' : 1, 'src' : "" } ] ] 'gallery2': [ 'images': [ { 'id' : 1, 'src' : "" } ] ] } regardless of which direction I use... my main question is this: I have two variables which are the following: Code: galVariable = (i = location.hash.match(/#set=(.+)&photo=(\d+)/)) ? i[1] : null; galArray = _gallery2.images; I'm unfamiliar with a way of doing something along the lines of this, and having it actually work: Code: galVariable = (i = location.hash.match(/#set=(.+)&photo=(\d+)/)) ? i[1] : null; galArray = galVariable.images; obviously "galVariable" would be undefined... as i'm trying to get "_gallery2.images" but trying to use "galVariable" to point to "_gallery2" I'm familiar with solving a problem like this in PHP, not so much javascript. is there a JS function that can be used to ensure that it's using whatever "galVariable" equals to, and then that array's content is? Or is there a different way to go on about this. thanks in advance. if anyone needs some more clarification. please feel free to ask, and I'll respond asap. I have var att=7,8,9 how can I convert these to an array so I can loop through the values? Hello, 1. I have the following script: function selectThis(id){ var locationstring = "process.php?value=" + id; ///// locationstring += "&bs=" + $('#bs_' + id).attr("selectedIndex"); locationstring += "&ad=" + $('#ad_' + id).attr("selectedIndex"); locationstring += "&ns=" + $('#ns_' + id).attr("selectedIndex"); locationstring += "&wt=" + $('#wt_' + id).attr("selectedIndex"); locationstring += "&in=" + $('#in_' + id).attr("selectedIndex"); document.location.href = locationstring; } With it I am getting a string with the value of the clicked button from an array and some other select field values that are listed in the same set of mysql results as the clicked button. BUTTON: <input name="button" type="button" onclick="selectThis('<? echo $rows['id']; ?>');" value=""> At the end of the script the page is redirected to in the script mentioned page with following string: process.php?value=1&bs=2&ad=3&ns=4&wt=5&in=6 (values are not real, so whatever the chosen value of the select field should apear in the link.) THIS WORKS FINE. 2. My problem is following: In order to be able to use var locationstring in other scripts as well I have removed var and have made it global. function selectThis(id){ locationstring = "process.php?value=" + id; ///// locationstring += "&bs=" + $('#bs_' + id).attr("selectedIndex"); locationstring += "&ad=" + $('#ad_' + id).attr("selectedIndex"); locationstring += "&ns=" + $('#ns_' + id).attr("selectedIndex"); locationstring += "&wt=" + $('#wt_' + id).attr("selectedIndex"); locationstring += "&in=" + $('#in_' + id).attr("selectedIndex"); } The problem is that later on when I use locationstring variable in other script I get only first variable in the string process.php?value=1 instead of process.php?value=1&bs=2&ad=3&ns=4&wt=5&in=6 3. Is it possible to make locationstring variable global and in a single line in order to be able to use the full string in other scripts or is there another solution. Thank in advance. Hi, I am trying to search an array for a string and then if it is found alert it. I have found examples of how to iterate the array and how to use .IndexOf to return a true false statement as to whether the array includes the string, but i don't know what to do after that and how to display the string if its found. Any help appreciated.. Thanks Hello. I need help PLEASE ! I need to convert a string Quote: lalaw|lalaw1|lalaw2|lalaw3 in to Array The values I have in variable "tables" I want to create variable list which takes values from "tables" Than I want to split this, and put each value in to new array: I've started with: Code: var list = "tables"; var listArray = list.split("|"); for(i=0; i < listArray.length;i++) { //? how to put now values in to array? } Best Regards Leos Hey. I have a string and I can return say, the fourth letter with output[3], but i can't assign a value to it for some reason: I am trying to make hang man Code: <!DOCTYPE html> <html> <head> <script src="jquery.min.js"></script> <script> $(document).ready(function(){ var word, output, wordLength, stringLength, newLetter, letters; var i, n, k, health, wcheck; word = "word"; output = ""; letters = ""; wordLength = word.length; for(i = 0; i < wordLength; i++){ output += "_"; } $("#inputLetters").keyup(function(){ newLetter = $("#inputLetters").val(); $("#inputLetters").val(""); letters += newLetter; wcheck = false; for(i = 0; i < wordLength; i++){ if(word[i] == newLetter){ wcheck = true; output[i] = newLetter; } } if(wcheck = false){ health++; } $("#letters").html(output + "<br />" + letters); }); }); </script> </head> <body> <span id="letters"> </span> <br /> <br /> <input id="inputLetters" type="text" /> </body> </html> Hello All, I have a string array in a class file that contains 5 values, which I need to display on the jsp page. but when I send it to the javascript it prints [Ljava.lang.String;@104f8b8 . what do i do about it. I am a new to javascript. Is there a way to get my array data from the response object. Please help. Hi all, Forgive my ignorance, I'm quite new to Javascript... I'm looking to do two things: the first is to move elements of an array to the next index using a function. I have managed to do this like so: Code: var letters = ['h','i','j','k']; function moveElements(anArray){ var newArray = anArray; newArray.unshift(newArray[newArray.length-1]); newArray.pop(); } moveElements(letters); (this may or not be great, but it seemed to work) ... but I want the original array to reset to its original state if the function is called again (thus producing the same answer as opposed to shifting the elements again). Secondly, and this is the bit I'm really struggling with: I want to compare each letter of a string to elements within an array and display the indices. For instance: Code: var myArray = ['a', 'b', 'c', 'd', 'e', 'f']; var text = 'dead'; Hence I want the result '3,4,0,3'. I know the answer will use for and if statements but I can't wrap my head around it at all. Help would be appreciated, thank you. |