We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51760
    • 6 Posts
    Hello,
    let me start by saying that I am new to Modx but after working with it for a few weeks, I am already a big fan.

    The scenario i am facing is as follow:
    I want to encrypt some data (like username and password) for which I already have the code in a js file.
    The easy way to use the file (and its functions) would be to load it in a <script> tag, and then use the encrypt() function when i need it.

    The problem with that is the file is now accessible just by looking at the "page source".

    I have been trying, but the chunks and TV only deal with html, and the snippets with PHP (and only on initial page load).

    Question is:
    How can I either

    • hide the <script> tag
    • create some link to the external file so i can access its functions when needed
    • or is there some type of a plugin that would help me accomplish this


    I am using revolution 2.4.2-pl

    Thank you in advance

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      If I'm understanding you, no matter how you do it, either the path to the JS file will be in the source, or the actual JS code will be in the source -- otherwise the browser isn't going to have access to the JS code. The JS code has to be available to the browser via URL, so it will always be accessible to anyone.

      I think your best bet may be to convert the JS code to PHP and put it in a snippet. That way it will never be visible.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 51760
        • 6 Posts
        Quote from: BobRay at Feb 04, 2016, 05:57 AM
        If I'm understanding you, no matter how you do it, either the path to the JS file will be in the source, or the actual JS code will be in the source -- otherwise the browser isn't going to have access to the JS code. The JS code has to be available to the browser via URL, so it will always be accessible to anyone.

        I think your best bet may be to convert the JS code to PHP and put it in a snippet. That way it will never be visible.

        Thank you BobRay for the quick response,

        Forgive me if I ask, but it is my understanding that a snippet is executed on page load. Once the page is loaded, you cannot use the snippet anymore but you can call another snippet by redirecting to a (no template) page using an ajax rest method, which works great except the URL shows in the address bar during the redirect. in all i did so far, it has not been an issue, but it will be an issue in this case if the username and password are displayed in the address bar.

        Also, the js file only contains a couple of functions. If I convert the js file to PHP and put it in a snippet for that resource (page), how can I call the functions when needed after the page is loaded?

        another thought, would it be possible to put the username and password in a hidden placeholder field which could be accessed by a TV or snippet, encrypted and returned back? if so, could you share an example on how to do it? [ed. note: pd5023 last edited this post 8 years, 3 months ago.]
          • 51760
          • 6 Posts
          Thank you BobRay for opening my eyes.

          I converted the js code to PHP and put in a snippet.
          Made a call to the snippet from a button's click() event, and it worked great.

          I was wrong believing a snippet only works on page load, it is in fact possible to call a snippet from anywhere in the page and at anytime.

          This is really great!

          Thanks again for the reply BobRay, even if it ended up being a "Lack of Knowledge" question.
            • 3749
            • 24,544 Posts
            I'm glad I could help. smiley
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting