Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 11 of 11

Thread: How to Replace Character in String

  1. #1

    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:

    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>
    Links would look like this:

    Code:
    <a href="event_registration.html?EventName=Great+Road+Race">Register for this Event</a>
    What's the best way to do the string replace?

  2. #2
    Looks like I need to use
    Code:
    query = query.replace("+", " ");
    but I can't get the global modifier to work (/g).

  3. #3
    Code:
    var pluses = new RegExp("[\+]{1,}", "g");
    query = query.replace(pluses, " ");

    hope this helps

  4. #4
    Thanks! I did finally get something to work using this syntax:

    Code:
    query = query.replace(/\+/g, " ");

  5. #5

    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>

  6. #6
    Code:
    .....
    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]);
       } 
    }
    .......

  7. #7
    Thanks 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;

  8. #8
    document.EventRegistration.EventName.value = EventName[0];
    document.EventRegistration.EventDate.value = EventName[1];


    in this part of your code its as follows :
    Code:
    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);
    }
    so i guess you may even want to call EventName perhaps EventNameandDate
    Last edited by a tadster; July 25th, 2009 at 02:17 PM.

  9. #9
    You mean something like this?

    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>
    Thanks for the help on this - it's a useful piece of code for people (hopefully).

  10. #10
    looks good!

  11. #11
    I should mention that this code should be placed AFTER the HTML form since HTML pages are read from top to bottom.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012