2012-07-25 46 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