Results 1 to 11 of 11
-
July 22nd, 2009, 01:53 AM #1519Registered User
posts
How to Replace Character in String
I have some code that grabs the variable value on the end of a query string, and I want to deal with phrases (such as "Great Road Race") by placing a plus (+) sign between the words (like "Great+Road+Race) and then run a string replace to swap in a blank space. Here's the existing code:
Links would look like this:Code:<script type="text/javascript"> 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]; } else { return "";// Don't display "undefined" } } } // There is additional script below the form </script>
What's the best way to do the string replace?Code:<a href="event_registration.html?EventName=Great+Road+Race">Register for this Event</a>
-
July 22nd, 2009, 02:35 AM #2519Registered User
postsLooks like I need to use
but I can't get the global modifier to work (/g).Code:query = query.replace("+", " ");
-
July 22nd, 2009, 07:21 AM #3Code:
var pluses = new RegExp("[\+]{1,}", "g"); query = query.replace(pluses, " ");
hope this helps
-
July 22nd, 2009, 09:08 PM #4519Registered User
postsThanks! I did finally get something to work using this syntax:
Code:query = query.replace(/\+/g, " ");
-
July 22nd, 2009, 09:35 PM #5519Registered User
posts
How can the following code be modified to provide the value for a second name/value pair (EventDate=5-1-10) as well?
Code:<a href="event_registration.html?EventName=Great+Road+Race&EventDate=5-1-10">Register for this Event</a>
Code:<script type="text/javascript"> function getIdFromQstring() { var url = document.location + '';// Insures string url = url.replace(/\+/g, " "); // Replace plus signs with spaces q = url.split('?'); if (q[1]) { var pairs = q[1].split('&'); for (i=0; i<pairs.length; i++) { var keyval = pairs[i].split('='); if (keyval[0] == 'EventName') { var v = keyval[1]; break; } } } if (v) { return v; } } var EventName = getIdFromQstring(); if (EventName) { // Pass the value to the hidden form field (will be invisible in source code) document.EventRegistration.EventName.value = EventName; //alert(EventName); } </script>
-
July 24th, 2009, 03:05 AM #6Code:
..... var pairs = q[1].split('&'); var v = new Array(); for (i=0; i<pairs.length; i++) { var keyval = pairs[i].split('='); if (keyval[0] == 'EventName' || keyval[0] == 'EventDate') { v.push(keyval[1]); } } .......
-
July 24th, 2009, 05:07 PM #7519Registered User
postsThanks tadster - that gets us part of the way there. I think we end up with a "value,value" pair though, and I'd like to stick each value in its own hidden form field:
document.EventRegistration.EventName.value = EventName;
document.EventRegistration.EventDate.value = EventDate;
-
July 25th, 2009, 02:11 PM #8
document.EventRegistration.EventName.value = EventName[0];
document.EventRegistration.EventDate.value = EventName[1];
in this part of your code its as follows :
so i guess you may even want to call EventName perhaps EventNameandDateCode:var EventName = geIdFromQstring(); if (EventName) { // Pass the value to the hidden form field (will be invisible in source code) document.EventRegistration.EventName.value = EventName[0]; document.EventRegistration.EventDate.value = EventName[1]; //alert(EventName); }Last edited by a tadster; July 25th, 2009 at 02:17 PM.
-
July 25th, 2009, 03:34 PM #9519Registered User
postsYou mean something like this?
Thanks for the help on this - it's a useful piece of code for people (hopefully).Code:<script type="text/javascript"> function getIdFromQstring() { var url = document.location + '';// Insures string url = url.replace(/\+/g, " "); // Replace plus signs with spaces q = url.split('?'); if (q[1]) { var pairs = q[1].split('&'); var v = new Array(); for (i=0; i<pairs.length; i++) { var keyval = pairs[i].split('='); if (keyval[0] == 'EventName' || keyval[0] == 'EventDate') { v.push(keyval[1]); } } } if (v) { return v; } } var GetValues = getIdFromQstring(); if (GetValues) { document.EventRegistration.EventName.value = GetValues[0]; document.EventRegistration.EventDate.value = GetValues[1]; //alert(GetValues[0]); } </script>
-
July 26th, 2009, 12:07 PM #10
looks good!
-
July 26th, 2009, 06:41 PM #11519Registered User
postsI should mention that this code should be placed AFTER the HTML form since HTML pages are read from top to bottom.

Reply With Quote

Bookmarks