+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

Thread: replace \

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    replace \

    I have a JS function that seems to be working fine until a value is added to a hidden text box. The function causes no problems until a value is added to the txt_filepath textbox, which has visibility:hidden style added to it.

    I think the problem is linked to the fact that the value in the textbox is in the form of \1\randomletters. I think it's the \'s that are causing the problem. How would I do a replace on them in JS?

    The error I get in Firefox is AddTask is not defined.

    The JS function is as follows:-
    Code:
    function AddTask(){
    var tr_box = document.getElementById('tr_box_start1');
    //alert(tr_box);
    if(tr_box!=null){
    document.getElementById('tr_box_start1').style.display='none';
    document.getElementById('tr_box_start2').style.display='none';
    }
    document.getElementById('ctl00_main_content_update_progress').style.display='';
    document.getElementById('update_text').innerHTML='Adding Task....';
    var pcuser = document.getElementById('ctl00_hdn_pcuser').value;
    var defaulttask = document.getElementById('ctl00_main_content_ddl_defaulttask').value;
    if(defaulttask=='') defaulttask=0;
    var visitorref = document.getElementById('ctl00_main_content_ddl_visitorref').value;
    if(visitorref=='') visitorref=0;
    var introref = document.getElementById('ctl00_main_content_ddl_introref').value;
    if(introref=='') introref=0;
    var cat = document.getElementById('ctl00_main_content_ddl_category').value;
    var sendto = document.getElementById('ctl00_main_content_ddl_sendto').value;
    var dtDue = document.getElementById('ctl00_main_content_txt_datedue').value;
    var tmSpent = document.getElementById('ctl00_main_content_txt_timespent').value;
    var title = document.getElementById('ctl00_main_content_txt_subject').value;
    //alert(title);
    var filepath = document.getElementById('ctl00_main_content_txt_filepath').value;
    filepath=escape(filepath);
    alert('filepath: '+filepath);
    var note=FTB_API['ctl00_main_content_ftb_mess'].GetHtml();
    //alert(note);
    //return false;
    TaskAdd.AddTask(pcuser,defaulttask,visitorref,introref,cat,sendto,dtDue,tmSpent,title,filepath,note,OnAddTaskComplete,OnError,OnTimeout);
    }
    
    Any ideas?

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    The backslash is an escape character in JS, so that could possible be causing the problem.

    Here's how to replace in JS: JavaScript replace() Method

    To keep the backslash in the string, simply replace it with two backslashes.
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    That's what I think J, but the problem is getting the syntax right on the replace function.
    This gives an error of unterminated regular expression literal
    Code:
    document.write(str.replace(/\/,"\\"));
    
    If I try this:-
    Code:
    var str="Visit \Microsoft!";
    document.write(str.replace(/\\/,\\\\)
    
    It just returns "Visit Microsoft!" when I would expect it to return "Visit \\Microsoft!"

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Try this:
    Code:
    document.write(str.replace(/\\/g, "\\\\");
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    Nope. It doesn't work if the original string just has a single \ in it.

    So, this:-
    Code:
    var str="Visit \\1\\Microsoft!";
    document.write(str)
    
    gives Visit \1\Microsoft!

    If I do:-
    Code:
    var str="Visit \\1\\Microsoft!";
    document.write(str.replace(/\\/g,"\\\\"));
    
    it gives Visit \\1\\Microsoft!

    If it starts with:-
    Code:
    var str="Visit \1\Microsoft!"
    
    then it doesn't do a replace.

    Any other ideas?

  6. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    What about this:
    Code:
    document.write(str.replace('\\', '\\\\');
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  7. #7
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    Nope. Still doesn't work.

    There must be a way of doing this, surely?

    If the original string just has a single \ then it just gets rid of it.

    This is very annoying..

  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    1,724
    Blog Entries
    10
    Rep Power
    11

    From what I've found it seems it's not possible to do this with Javascript.

    The original string must have double slashes to do a replace.

  9. #9
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    I think I'm getting closer, R...but still getting weird results:
    Code:
    <script type="text/javascript"><!--
    
    var esc = {"\\":/\\/g, n: /\n/g, t: /\t/g, r: /\r/g, b: /\x08/g, s: /\x73/g}
        var Str= "\1\randomletters";
    var newStr = Str;
    for(e in esc){
      newStr = newStr.replace(esc[e], "\\"+e);
    }
        alert(newStr);
    //-->
    </script>
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  10. #10
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Hey R,

    Maybe if you convert to forward slashes first:
    Code:
            function back2forward(dataStr) {
                return dataStr.replace(/\\/g, "/");
            }
    
    Then, handle the string how you need to...then when you're done, convert it back:
    Code:
            function forward2back(dataStr) {
                return dataStr.replace(/\//g, "\\");
            }
    
    I'm running out of ideas on this one..
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread
Page 1 of 2 1 2 LastLast

Tags for this Thread

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

SEO by vBSEO