Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:18:30+00:00 2026-06-17T19:18:30+00:00

I want to do something like this : <script> var text = this is

  • 0

I want to do something like this :

<script>
var text = "this is some sample text that i want to replace";
var new_text = text.replace(/'/g, "\\'");
var new_text = text.replace(/"/g, "\"");
document.write(new_text);
</script>

but as long as there is a ” in my var text var text = "this is some sample "text" that I want to replace"; it breaks my javascript so that I cannot replace it.

My var text comes from a modx CMS tiny mce field so it is a kind of $ displayed like this [[*content]].

And this is my modx template code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr"> 
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>[[*titrepage]]</title>
<style type="text/css">
div.code_view {
    height:auto;
    }

div.code_demo {

    /* margin:1em auto 1.4em; */
    border:1px solid #ddd;
                width: 535px;
                color: #112433;

    }

div#content div.code_demo ul.demoLinks {
    margin:6px 6px 10px;
    }

div#infoDiv { 
   padding:0 8px 8px;
   }



div#infoDiv h4 {
    margin:4px 0;
    font-size:1.1em;
    }

</style>
<script src="http://aurorafilms.eskem-studio.com/assets/templates/aurora/js/dw_contentswap.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">

function init_ContentSwap() {
    var swap1 = {
        restoreDefault: false,
        content: {
            'old': "<h4>Synopsis</h4><p>[[*synopsis]]</p>",
            'infos': '<h4>Infos</h4><p>[[*infos]]</p>',
                                            'presse': "<h4>Presse</h4><p>[[*Presse:jsonEncode]]</p>",
            'festivals': "<h4>Festivals</h4><p>[[*festival]]</p>"


        }
    }

    dw_ContentSwap.setup(swap1);
}

dw_Event.add(window, 'load', init_ContentSwap);

</script></head>
<body>  
<div id="page">    <ul class="demoLinks">
        <li style="z-index: 10; width: 80px; position: relative; "><a href="#old" class="showInfo old">Synopsis</a></li>
        <li style="z-index: 9; width: 80px; position: relative;  left: -20px;"><a href="#infos" class="showInfo infos">Infos</a></li>
        <li style="z-index: 8; width: 80px; position: relative;  left: -40px;"><a href="#presse" class="showInfo presse">Presse</a></li>
        <li style="z-index: 7; width: 80px; position: relative;  left: -60px;"><a href="#festivals" class="showInfo festivals">Festivals</a></li>

    </ul><div id="content" style="float: left;" >
            <div id="text">
<!-- demo -->

<div class="code_demo">


    <div id="infoDiv"><h4>Synopsis</h4><p>[[*synopsis]]</p></div>
</div>


<!-- end demo -->

<p class="clearer">&nbsp;</p></div> </div>  </div>    



</body>
</html>

So what is the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:18:31+00:00Added an answer on June 17, 2026 at 7:18 pm

    The problem is, that you’re trying to use the content from your resource as a javascript string (injected by the modx parser). But hardcoded strings in javascript must follow some rules (no or only escaped newlines, proper quoting). So simply putting some quotes around it will only help in the simplest cases.

    To fix your problem, try the following:

    1. create a new snippet, called jsonEncode with the following content:

    <?php
    return json_encode($input);
    

    2. change the javascript code, where you want to use your resource-content to something like:

    var text = [[*content:jsonEncode]];
    

    Links:

    • Input and Output Filters (Output Modifiers)
    • Pass a PHP string to a Javascript variable (and escape newlines)

    update

    Seeing the added code, I’d suggest a different approach. As what I described above will probably break if you’re using nested modx-variables. I think the savest way would be to render whatever content you need accessible in js into a hidden div. And extract the innerHTML from that element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: require 'erb' @var = 'test' template =
I have something like this <button onclick=JavascriptFunction();>HTTPPost Button</button> <script type=text/javascript language=javascript> function JavascriptFunction() {
Im trying to accomplish something like this. I want to add the text stored
I have this jsonp function: function jsonp(url, callback) { var script = document.createElement(script); script.setAttribute(type,text/javascript);
I want something like this: abcdab.search(/a/g) //return [0,4] Is it possible?
I want to parse something like this: Hi [{tagname:content}] [{tag1:xnkudfdhkfujhkdjki diidfo now nested tag
I want to run something like this: For a = 0 To 4 For
I want to do something like this in SQL Server: SELECT pt.quantity AS 'QTY'
I want to do something like this private <T extends List<?>> List<?> getFirstFiveElements(T list)
I want to do something like this: class Dictable: def dict(self): raise NotImplementedError class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.