JJ2357
asked on
How can I use jquery to parse out parameters from an encoded url with custom separators and place them into a string with pre-defined positions?
My goal is to extract values from name-value pairs within a URL and return those values into a string that can be inserted into a web tracking tag. Th using a js function. The string has fifteen positions separated by "-_-". No matter where each name-value pair appears within the URL, values most always be inserted into the same position within the tracking tag string. For example, the value(s) for the name apple must always appear in the first position, the value(s) for the name banana must always appear in the second position, and the value(s) for the name watermelon must always appear in the fifth position.
The URL will have a pound sign followed by an indefinite number of name-value pairs (up to 15). The name value pairs, connectors, and separators are encoded. Values are strings that may be joined by "+" symbol. Finally, Names may appear multiple times within the URL, each time with a different value. However, since names only exactly one corresponding spot within the tracking tag string, the associated values must be joined into the same position (using "+").
Example URL:
http:www.mysite.com#currentPage=1&c%5B%5D=e%3A1119//e+apple//%3Aeq//red&c%5B%5D=e%3A1119//e+banana//%3Aeq//yellow+black&c%5B%5D=e%3A1119//e+watermelon//%3Aeq//red&c%5B%5D=e%3A1119//e+watermelon//%3Aeq//black
Example function using the tracking tag string created from above:
cmCreateElementTag("Tracki ng Parent", "Tracking Child","red-_-yellow+black -_--_--_-r ed+black-_ --_--_--_- -_--_--_-- _--_--_-") ;
I'd like to use jquery to extract the values from the URL and place them in the right position within the tracking tag string.
The URL will have a pound sign followed by an indefinite number of name-value pairs (up to 15). The name value pairs, connectors, and separators are encoded. Values are strings that may be joined by "+" symbol. Finally, Names may appear multiple times within the URL, each time with a different value. However, since names only exactly one corresponding spot within the tracking tag string, the associated values must be joined into the same position (using "+").
Example URL:
http:www.mysite.com#currentPage=1&c%5B%5D=e%3A1119//e+apple//%3Aeq//red&c%5B%5D=e%3A1119//e+banana//%3Aeq//yellow+black&c%5B%5D=e%3A1119//e+watermelon//%3Aeq//red&c%5B%5D=e%3A1119//e+watermelon//%3Aeq//black
Example function using the tracking tag string created from above:
cmCreateElementTag("Tracki
I'd like to use jquery to extract the values from the URL and place them in the right position within the tracking tag string.
ASKER
Thanks, Rob. Works great, with the exception of prepending "-_-" to the beginning of the string and not including double quotes around the string.
Result:
Additionally, in the event that the URL contains an unencoded name-value pair, the function fails. For example, if you append &name=value to the end, an undefined error is returned.
Result:
-_-red-_-yellow+black-_--_--_-red+black-_--_--_--_--_--_--_--_--_-
Expected result:
"red-_-yellow+black-_--_--_-red+black-_--_--_--_--_--_--_--_--_-"
Additionally, in the event that the URL contains an unencoded name-value pair, the function fails. For example, if you append &name=value to the end, an undefined error is returned.
Ok i've updated the code as the index was out by 1 (zero based array - first element is at index 0) http://jsbin.com/xukaso/1/edit?js,console#J:L1
As for the additional variables in the querystring, given that they weren't in your original spec, I haven't accounted for them. So What are the variables that need to be parsed? Will they always be the c[] variable or ?
As for the additional variables in the querystring, given that they weren't in your original spec, I haven't accounted for them. So What are the variables that need to be parsed? Will they always be the c[] variable or ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
works like a charm, tx.
not a problem. Sounds like that's solved it for you. If so, go ahead and close the question: http://support.experts-exchange.com/customer/portal/articles/608621
ASKER
Sorry for not being clear in the original. Thanks for the extra work.
Open in new window