title: How to esacpe JavaScript within jQuery variable?
body: <p>I'm working on a JavaScript/jQuery draggable widget manager. Currently, this is working great on manually moving the containers and saving the sort value to a DB (via PHP). I'm putting this together for a simple way to position various ad zones for a client.</p>
  
  <p>The problem is now, when I'm trying to move my zones, jQuery breaks down when there is JavaScript ad code within the zone. The point of failure is when I'm trying to select the class container to get the id of the zone.</p>
  
  <pre><code> var items=[];  
    $('.column').each(function(){  
        var columnId=$(this).attr('id');  
        $('.dragbox', this).each(function(i){ 
            var item={  
                id: $(this).attr('id'),  
                collapsed: 0,  
                order : i,  
                column: columnId  
            };  
            //Push item object into items array  
            items.push(item);  
        });  
    }); 
  </code></pre>
  
  <p>After creating this array, it's passed via JSON to my update script. The problem is that nothing is getting created into the array when I have ad server code. Here is the example of what it would look like (HTML)</p>
  
  <pre><code>&lt;div class="dragbox" id="item1"&gt;
  &lt;h2&gt;&lt;span class="configure" &gt;&lt;a href="#" &gt;Configure&lt;/a&gt;&lt;/span&gt;Handle 1&lt;/h2&gt;
  &lt;div class="dragbox-content" &gt;
      &lt;!-- Panel Content Here --&gt;
      &lt;script type='text/javascript'&gt;&lt;!--//&lt;![CDATA[
  var m3_u = (location.protocol=='https:'?'https://ads.mytestsite.com/_ads/delivery/ajs.php':'http://ads.mytestsite.com/_ads/delivery/ajs.php');
  var m3_r = Math.floor(Math.random()*99999999999);
  if (!document.MAX_used) document.MAX_used = ',';
  document.write ("&lt;scr"+"ipt type='text/javascript' src='"+m3_u);
  document.write ("?zoneid=24");
  document.write ('&amp;cb=' + m3_r);
  if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
  document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
  document.write ("&amp;loc=" + escape(window.location));
  if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
  if (document.context) document.write ("&amp;context=" + escape(document.context));
  if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
  document.write ("'&gt;&lt;\/scr"+"ipt&gt;");
  //]]&gt;--&gt;&lt;/script&gt;&lt;noscript&gt;&lt;a href='http://ads.mytestsite.com/_ads/delivery/ck.php?n=a7d957c0&amp;cb=&amp;n=a7d957c0' border='0' alt='' /&gt;&lt;/a&gt;&lt;/noscript&gt;
  
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>If I remove the <code>'&lt;script type='text/javascript'&gt;'</code> things seem to work better. I've done some testing with <code>&lt; / &gt; ' " ! -- [</code> chars and those all seem to be fine. </p>
  
  <p>As you can see, I'm only trying to grab the id of the class container and pass that to JSON, but the content is within this container, and I think it's borking things up. I also tested with Error Console in Firefox and it's not reporting any errors.</p>
  
  <p>Any suggestions are greatly appreciated!</p>
  
  <p>hanji</p>