Results 1 to 4 of 4

Thread: Replacing White space in variables

  1. #1
    Join Date
    Jul 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Replacing White space in variables

    I have a series of variables which ultimately form a hyperlink. During the document write process for the actual hyperlink if there are spaces found the string breaks and does not include any characters past the space. I am using a replace function on the input variable to accomodate for this and add "%20", however just found the way I wrote it, this is only replacing the first instance... Any thoughts out there on how to do this the right way to catch all spaces in the string?

    <script language="javascript" type="text/javascript">
    var path = "http://URL/";
    var report = reportSelect.value;
    var reportName = report.replace(/ /,"%20");
    document.write(report + "<BR/>" + "<A href=" + path + reportName + ".xml" + " target=viewer>" + "Show Report" + "</A>")
    </script>

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Just use escape()?

  3. #3
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    report.replace(/ /g,"%20");
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Both previous responses in this thread appear workable. However, to conform to present standards (giving you the best shot at future compatibility), one should employ the encodeURI function. For more info on its use, see:

    http://www.w3schools.com/jsref/jsref_encodeURI.asp

    or your favorite resource for info about built in javascript functions.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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