2012-07-25 62 views
0

我正在研究JavaScript/jQuery可拖动小部件管理器。目前,这对于手动移动容器并将排序值保存到数据库(通过PHP)非常有效。我将它们放在一起,以便为客户定位各种广告区域。如何在jQuery变量中实现JavaScript?

现在的问题是,当我试图移动我的区域时,jQuery在区域内存在JavaScript广告代码时发生故障。失败的关键是当我试图选择类容器来获取区域的ID。

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); 
     }); 
    }); 

创建此数组后,它通过JSON传递给我的更新脚本。问题是,当我有广告服务器代码时,没有任何东西被创建到数组中。这里是它会是什么样子(HTML)的例子

<div class="dragbox" id="item1"> 
<h2><span class="configure" ><a href="#" >Configure</a></span>Handle 1</h2> 
<div class="dragbox-content" > 
    <!-- Panel Content Here --> 
    <script type='text/javascript'><!--//<![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 ("<scr"+"ipt type='text/javascript' src='"+m3_u); 
document.write ("?zoneid=24"); 
document.write ('&cb=' + m3_r); 
if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used); 
document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : '')); 
document.write ("&loc=" + escape(window.location)); 
if (document.referrer) document.write ("&referer=" + escape(document.referrer)); 
if (document.context) document.write ("&context=" + escape(document.context)); 
if (document.mmm_fo) document.write ("&mmm_fo=1"); 
document.write ("'><\/scr"+"ipt>"); 
//]]>--></script><noscript><a href='http://ads.mytestsite.com/_ads/delivery/ck.php?n=a7d957c0&cb=&n=a7d957c0' border='0' alt='' /></a></noscript> 

</div> 
</div> 

如果我删除'<script type='text/javascript'>'事情似乎更好地工作。我已经用</> ' " ! -- [字符做了一些测试,并且这些看起来都很好。你可以看到,我只是试图抓取类容器的id,并将其传递给JSON,但内容在这个容器中,我认为这是一件令人兴奋的事情。我还在Firefox中使用错误控制台进行测试,并且不报告任何错误。

任何建议,非常感谢!

韩纸

+1

umm你先串接字符串,当你完成后,你使用document.write一次 – Ibu 2012-07-25 20:01:52

+0

我没有关注..你能解释一下吗?在$('。dragbox',this)之后连接整个广告代码部分.each(function(i){? – hanji 2012-07-25 20:37:49

+0

看起来故障的关键在于广告代码中的document.write()。删除这些应用程序工作正常张贴到数据库 – hanji 2012-07-25 21:57:09

回答

1

每当浏览器的JavaScript解释过程中遇到</script>,即使是在一个字符串,它把它作为一个关闭标签。

相关问题:Script tag in JavaScript string

通常使用的技术是使用连接运算符:

变种测试= '...... </scr'+'ipt> ......';

+0

另一个问题是,我需要包括JavaScript来呈现在该框中,因此广告正确显示,并给予用户拖动容器,将其更改为'将打破,除非我做一些document.write的东西,我希望有一个简单的解决方案,可以排除'content'部分,只是使用id,或者在引用该类容器的时候以某种方式使用编码等。 – hanji 2012-07-25 20:36:50