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"> </p></div> </div> </div>
</body>
</html>
So what is the best way to do this?
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
jsonEncodewith the following content:2. change the javascript code, where you want to use your resource-content to something like:
Links:
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
innerHTMLfrom that element.