We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23383
    • 138 Posts
    hello,

    I’m making a Snippet that depends on javascript, and I’d like to have one of my snippet parameters to edit some js file.
    I was wondering what’s the best way to do it ? (I mean performance/User-friendly -- for people who need to work with this snippet)
    I tought :
    1) Make a js document in modx doc tree, opening it in my snippet with $jsDoc = $modx->getDocumentObject(’id’,id_of_my_js_file); and then editing the $jsDoc[’content’]
    2) Make a chunk with the script content, either editing it with the $modx->getChunk(’jschunk’); or parsing it with $modx->parseChunk(’jschunk’, $array_of_parameters, ’[+’, ’+]’);
    (of course, my js content will have placeholders like [+some_parameter+] )
    3) Include a JS file from the file system, and editing with php.

    I think the Chunk method could be more performant, am I right ? Maybe you have another solution ?
      • 23383
      • 138 Posts
      In fact, none of the ideas you have can fit :
      The JS ’file’ has to correspond to a custom user chunk (the user should edit it, so It should edit the JS), so I can’t generate it.
      But I’d like my users could easily switch some parts of the JS, and that could be done only (I think) with a Snippet Parameter.
      That’s why my snippet has to manipulate the JS file. I’ll show an exemple as soon as possible.
        • 3749
        • 24,544 Posts
        Can you give the users specific choices, or as an alternative, let them fill in specific fields that would alter the js behavior or do they really need to edit the js themselves? Letting users edit raw js seems like a *huge* security risk.

        Bob
          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
          • 23383
          • 138 Posts
          Yes, I’ll let the users (webusers don’t have access to this part) some choices, which will have impact on the JS.
          Thank you for reminding me the security risk smiley

            • 3749
            • 24,544 Posts
            There’s probably a more elegant and easier kissway, but the first thing that comes to mind is a form that posts to itself and, on post-back, saves the data in SESSION variables and sends the user to another page where your snippet is. The snippet gets the data and outputs the JS on the fly.

            Bob
              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