×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    14

    Escape character ( \ ) problem

    So I'm having a problem with escape characters in a series of MWL functions - it's kind of hard to explain, but I'll do my best!

    I have an MWL timer function, which ticks once, and then calls iterateClass, and another repeating timer, which itself calls iterateClass. This needs to be created in javascript and set via the setAttribute function, because the length of the timer is dynamic. Obviously, because there are several nested functions here, I need to escape the quotation characters, sometimes more than once. So, what I end up with is:

    actionString += "mwl.timer('timer2', '" + ((digit3 * 1000) + 1000) + "', '1', 'mwl.iterateClass(\\\'#timer_digit2\\\', \\\'clock_digit\\\', \\\'prev\\\', \\\'10\\\', \\\'true\\\', \\\'\\\'); mwl.timer(\\\'timer2\\\', \\\'10095\\\', \\\'0\\\', \\\'mwl.iterateClass(\\\\\\\'#timer_digit2\\\\\\\', \\\\\\\'clock_digit\\\\\\\', \\\\\\\'prev\\\\\\\', \\\\\\\'10\\\\\\\', \\\\\\\'true\\\\\\\', \\\\\\\'\\\\\\\');\\\'); ');\n";
    I know that looks pretty awful, and is probably hard to make sense of, but in the local simulator it does work - the first timer goes off once, iterates the class correctly, and then starts a new timer which periodically ticks that same class.

    When this is properly parsed by the browser, the resulting code is as follows, which is correct:

    <td id="start_button_btc" onclick="mwl.timer('btc_game_timer2', '8000', '1', 'mwl.iterateClass(\'#timer_digit2\', \'clock_digit\', \'prev\', \'10\', \'true\', \'\'); mwl.timer(\'btc_game_timer2\', \'10095\', \'0\', \'mwl.iterateClass(\\\'#timer_digit2\\\', \\\'clock_digit\\\', \\\'prev\\\', \\\'10\\\', \\\'true\\\', \\\'\\\');\'); ');
    ">Start!</td>
    Note that the long strings of slashes have translated into groups of 3 slashes, as they should do.

    Ok so, hopefully this has been clear so far! The problem I am having is as follows:
    When running in the cloud preview simulator, this only works the first time. So the first time through this code, everything works nicely - the player clicks the button (the code for which is above), the <div> containing that button is hidden and the app progresses. When it comes back around (ie, this button is displayed again, for the second time), those groups of 3 slashes have been replaced with only 2 slashes: ie, the html in the browser the second time around is as follows:

    <td id="start_button_btc"
    onclick="mwl.timer('btc_game_timer2','10000','1','mwl.iterateClass(\'#timer_digit2\',\'clock_digit\',\'prev\',\'10\',\'true\',\'\');mwl.timer(\'btc_game_timer2\',\'10095\',\'0\',\'mwl.iterateClass(\\'#timer_digit2\\',\\'clock_digit\\',\\'prev\\',\\'10\\',\\'true\\',\\'\\');\');');">
    Start!</td>
    The groups of "\\\" in the final function have been replaced with "\\", which obviously doesn't work correctly when the button is clicked.

    So, my question is, is this something I'm causing somehow? It seems strange to me that this code would work the first time through, but not the second (and works constantly in local simulator mode). Is there any way to fix it? Or is there a better way to have nested MWL functions like this?

    Like I said, this is a pretty tricky problem, so hopefully I've explained it clearly. Any assistance on this problem will be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Re: Escape character ( \ ) problem

    Hi Icepick,

    I've been using similar nested mwl.iterateClass calls and for me it works. I hope this small test code helps you to find out, what causes the unexpected behavior.

    Here's a "Start timer" item, which disappears when clicked, and reappears via a nested mwl.iterateClass call, after a timeout.

    Code:
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css">
            .i0 {background-color: green}
            .i1 {background-color: lime}
        </style>
    </head>
    <body>
        <div id="a" class="i0">A</div>
        <div id="startTimer">
            <script type="text/javascript">
                function addSlashes(str) {
                    return str.replace(/\\/g, '\\\\')
                            .replace(/\'/g, '\\\'')
                            .replace(/\"/g, '\\"')
                            .replace(/\0/g, '\\0');
                }
                
                document.write('<div onclick="'
                        + "mwl.hide('#startTimer');mwl.timer('t1', '1000', '1', '"
                        + addSlashes("mwl.iterateClass('#a', 'i', 'next', 2, true, '"
                        + addSlashes("mwl.timer('t2', '1000', '1', '"
                        + addSlashes("mwl.show('#startTimer');mwl.iterateClass('#a', 'i', 'next', 2, true, '');")
                        + "');")
                        + "');")
                        + "');"
                        + '">Start timer</div>');
            </script>
        </div>
    </body>

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    14

    Re: Escape character ( \ ) problem

    Ah ha, thats fantastic. I've switched to using the addSlashes function (which is obviously a lot more reliable than doing them all manually!) and everything is working nicely now. Thanks for your help!

  4. #4
    Regular Contributor
    Join Date
    Jun 2010
    Posts
    161

    Re: Escape character ( \ ) problem

    Could you explain the function to me?

    I'm not familiar with regex and this chained replace within the return statement is also not clear to me.

    Can I use this to even functions within function within functions?

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Re: Escape character ( \ ) problem

    Sure, so the replace method simply returns a new string, which has a new replace method. Finally the last string is returned. Alternative way to write this would be:
    Code:
    function addSlashes(str) {
    	str = str.replace(/\\/g, '\\\\');
    	str = str.replace(/\'/g, '\\\'');
    	str = str.replace(/\"/g, '\\"');
    	return str.replace(/\0/g, '\\0');
    }
    In the example, the function is defined in global context (not inside another function), therefore it's available anywhere in the code.

    More about replace method and regular expressions:
    http://w3schools.com/jsref/jsref_replace.asp

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    5

    Re: Escape character ( \ ) problem

    Yes, you can use thing function just add it on you javascript and you can calling this function everytime.

    # Using function make you web apps it's good.

Similar Threads

  1. Receiving Escape 1B Problem in SMS
    By Jamgant in forum General Messaging
    Replies: 0
    Last Post: 2009-11-21, 05:04
  2. problem with escape sequences.
    By Dattanand in forum Symbian
    Replies: 3
    Last Post: 2008-06-21, 14:04
  3. Replies: 1
    Last Post: 2008-01-10, 15:43
  4. CRLF, _LIT8() and escape sequences problem?
    By nawkboy in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2003-04-03, 00:44
  5. CRLF, _LIT8() and escape sequences problem?
    By nawkboy in forum Symbian
    Replies: 0
    Last Post: 2003-04-03, 00:43

Posting Permissions

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