I have a string that contains n items. Each item start with a '@' and the
item itself does not contains the '@a'.
For example the string looks like: "@one@two@three@four"
I have to output this string as "one, two, three and four".
So in fact the first '@' can be removed, the next except the last replaces
by ", " and the last one by the word "and ".
Is there a simple way doing this?
I am looking for a javascript function that will parse a query string. Parameters are passed in the url: url?a=3&c=5&etc An array is returned that uses the variable name as the index.
I'm trying to parse a string from a URL and pass it as a parameter to another function. If that parameter meets certain conditions, I want hidden fields in my form to "unhide".
I am giving 3 usersr a specified url i.e. (http://DOMAINNAME.com?usr=uk or http://DOMAINNAME.com?usr=france) and based on that url, we'll parse the name and pass it as a parameter to a function. (i think i expressed that right??) Then we will display the proper shipping address depending on the parameter passed. My only problem now is, i'm horrible at writing regular expressions and programming in general. Do you or anyone else out there know of any good url parseing functions? I've been doing mostly project management throughout my career and never really got a chance to become fluent in javascript or programming for that matter.
I feel like I have been repeating this code lately, so I cleaned it up and am gonna show it here. This function returns an array (with hash names) containing all the get variables.
function parseGetVars() { var getVars = new Array(); var qString = unescape(top.location.search.substring(1)); var pairs = qString.split(/&/); for (var i in pairs) { var nameVal = pairs[i].split(/=/); getVars[nameVal[0]] = nameVal[1]; } return getVars; }A simple page to test this looks like<html> <head> <title>Test</title>
<script> function parseGetVars() { var getVars = new Array(); var qString = unescape(top.location.search.substring(1)); var pairs = qString.split(/&/); for (var i in pairs) { var nameVal = pairs[i].split(/=/); getVars[nameVal[0]] = nameVal[1]; } return getVars; } </script> </head>
<body> <script> var g = parseGetVars(); for (var i in g) document.writeln(i+'='+g[i]+'<br>'); </script>
I see that jQuery provides a function to turn an object into a set of URL query parameters: $.param({foo:"xxx", bar:"yyy"}) => "foo=xxx&bar=yyy" is there a function which does the opposite, i.e.parsing a query string into an object? The reason is that when I make an Ajax request, I want to take some parameters from the original page and include them in the new request, and modify others. I can get the original page's query string from location.search, which may contain, say, "?foo=xxx&bar=yyy" Now, suppose I want to submit an Ajax request with the same value of foo as the original page but a different value of bar, what's the cleanest way to do that? Remember that the original query string might have the two parts the other way round, i.e. "?bar=yyy&foo=xxx" If I need to write a function to split this myself, I know it's not a major undertaking, but I just wanted to see if I've missed something in the API.
I'm trying to parse a Json string received by $.getJSON(). I can receive the string but firebug gives me an "invalid label" error. I think it is because the string is an hash array with a number as first label, but I'm not sure. This is the string:
{"15":{"id":15,"x":0.4589937586135409,"y": 0.8324914620560137,"z":-0.9435707004102728,"rawData":"1256204002860 13 -442866538 18392602 647462767 314 1777206957 -1664784174 "}} and this is the non-working code:
I have an XML document that is returned which has an element named html. Inside of that element is a block of HTML wrapped with CDATA tags. I can alert the html variable that i create and see it has all of the data inside of it. So I want to parse through and grab certain things now. I'm just trying to get the element to return it's id to me, even though I know it ... because I kept getting the following error with other code.[code]
I have an XML document that is returned which has an element named html. Inside of that element is a block of HTML wrapped with CDATA tags. I can alert the html variable that i create and see it has all of the data inside of it. So I want to parse through and grab certain things now. I'm just trying to get the element to return it's id to me, even though I know it ... because I kept getting the following error.
I still continue to get this error with the current code below:
I need to parse an HTML string received from an AJAX request. I wrote a function that places the HTML string into an unappended (not added) <div> , which opens the string up to the DOM hierarchy. However, when I try to access the elements of this <div> , I get an error in the console that says root.getElementById is not a function. This tells me that I can't access any of the child nodes.
Here is what my script looks like:
function parseHTML(html) { var root = document.createElement("div"); root.innerHTML = html; // The error console stops at this line
I'm not entirely familiar with the norms and standard libraries of JavaScript so if the answer to this is yesterday's news, please ignore.
I'm trying to write a simple text formatting function to make headings proper case -- i.e. first letter of words capitalized.
I first tried this...
function inProperCase(s) { var result, i, k; i = 0; result = s.toLowerCase(); while ((k = result.indexOf(' ', i)) -1) { if (k - i 0) { result[i] = result[i].toUpperCase(); } i = k + 1; } if (result.length - i 1) { result[i] = result[i].toUpperCase(); } return result; }
...but it didn't work. To my amazement, you cannot set a character in a string by accessing that character with an index, like so:
s[i] = ch;
Must be because JavaScript strings are "Immutable" or "EtchedInStone" or something. (Just kidding, no flames, please. I'm sure it is more efficient.)
The only thing I could come up with was this (with some details, like the "compound" while predicate, modified into a more readable form after being called a "C-bigot" by a friend :)) which seems pretty heavy-handed with all the string slicing and concatting:
function inProperCase(s) { var result = s.toLowerCase(), i = 0, k = result.indexOf(' ', i); while (k -1) { if (k - i 0) { result = result.substring(0, i) + result[i].toUpperCase() + result.substring(i+1, result.length); } i = k + 1; k = result.indexOf(' ', i); } if (result.length - i 1) { result = result.substring(0, i) + result[i].toUpperCase() + result.substring(i+1, result.length); } return result; }
You call this as...
inProperCase(" madam i'm adam ");
...and you get...
" Madam I'm Adam "
Any suggestions for making this a bit more elegant?
BTW You may point me to a "library" with a super-efficient implementation of this function, but that is not the point. My issue is, how do we update single characters in a string (or single items in an array) in JavaScript? Is it something like...
I have a dynamically generated table, filled from a database by a perl application.
Each row represents a database record and has a 'status' and a unique 'id'.
What I want to do is create buttons to hide all rows with a particular status. The code to show/hide is relatively easy, but how do I turn them all off at once?
Several ideas I had:
1. Set the tr's 'id' attribute to the status (eg 'open') and then set that id to display: none. But that only turns off the first one.
2. Do the same but somehow loop over all elements looking for that id.
3. Set the tr's 'id' attribute to the status plus the data's id (eg 'open.24') then loop over all elements matching the status with a regexp.
4. Doing something else that I haven't thought of yet.
Right now, the array items print out without spaces like "Jane,John,Mary,Sue". I need to be able to work with the items individually so I can format them so they read "Jane, John, Mary and Sue". I thought I'd be able to do something like v[i] + ", " but I get an "undefined" when I try to alert for v[i]. Can someone point me in the right direction? Code:
Is there a way to do it so that when listnav initially appears on the page, no items are shown? In other words, is there a way to just have the alphabetized index show with no initial display of anything else?
I am using a drop down menu for a website I'm working on to display menu items under categories. I'm using the same code to do this on two different pages with the actual content of the menu's loading from a MySQL database using PHP scripts. [URL] On the menu.php page I left the code below in the mix which drops down the first category listed on the left. On the seasonal.php page I took the code below out and it no longer drops down any menu by default when the page is loaded. What I'd like to do is drop down nothing initially when the page is loaded but if someone opens up the "Cupcakes" category and clicks on an item it would keep that category open when it loads the item details in the center column. If you need to see more code let me know!
Here is the thing, I need to have parts of my website display different items depending on the browser (Netscape or Explorer). I have been using PHP to do this on 90% of the site, and it works great. However due to limitations on some pages (the shopping cart software) I cannot use PHP. When the page loads I can use Javascript, and I need it to load a specific header & footer for each browser type. I know that javascript cannot use SSI, so I would basically add everything into the one page and just have the script decide which chunk to add (the if or else statements get to decide I assume). The thing is I rarely use Javascript and when I do it has been extremely simple. here is the basic format I assume it will be using... (not in actual code though as I dont know javascript)
i'm after some script to display some html depending on what is selected in a list box the html i want to display are checkboxes so it's more stuff for a form i'm using lotus notes, so i have to use javascript.
i can get it to display a checkbox if a particular item is selected but it won't show up in place, it loads a new page with the check box the only thing on that new page.
function updateChecks() { string = ""; if (document._WEBRequest.dataReq.value == "blah") { string = "<input type="checkbox" name="check" />"; string += "Boundary"; } return string; } function writeChecks() { document.write(updateChecks()); } the writeChecks() function is called onChange for the listbox
also don't have much clue as to how to make it show more checkboxes if more than one item is selected in the listbox.
I'm wondering if there's a way to have multiple row carousel implemented.
I use jCarouselLite.. [url]
To be more specific, here's what I have:
And this is what I would like to accomplish:
I do realize that placing multiple items withing the <li></li> would be one approach, however I can't do that since the items are generated dynamically. The structure of my data (items) is an unordered list:
Using "float: left" & "clear: left" messes up the whole structure...
Any ideas how to display 9 items at a time on 2 rows?
What is stopping my code from displaying the time, date, and greeting from my stored variables?[code]...
I also tried placing paragraph tags, "", and '' ,respectively, inside the parenthesis as well as using '+variable+' instead of " . variable . "
The word "blah" was used to make sure the marquee tag was supported by browsers I have used to test this. So far I have only run into trouble with my document.write(stuff) not being displayed.
I have a problem with innerHTML. If I wrote document.getElementById('someid').innerHTML = "ok"; then it works But when i wrote document.getElementById('someid').innerHTML = "<sometext> ok"; it does not work. i.e. <sometext> is not visible If check on firebug / dom it display.. <sometext> ok </sometext> How do I print / display above string as it as.