2013-03-08 51 views
0

我想通過插入這段JavaScript代碼。谷歌:插入的Javascript到HTML中的Javascript

<script type='text/javascript'> 
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1362706866260-0'); }); 
</script> 

到這個JavaScript/HTML代碼:

var theDivs = { 
     "div1": makeSpecialBox("doublearticlewrapperadmp", 
       '<div class="articlewrapper" id="div-gpt-ad-1362706866260-0"> 
       <div class="entry-image"></div></div>'), 
     ... 
}; 

我想插入我的「入門圖像」分區之間上面列出的第一個代碼。我試圖簡單地做這樣的:

var theDivs = { 
     "div1": makeSpecialBox("doublearticlewrapperadmp", 
'<div class="articlewrapper" id="div-gpt-ad-1362706866260-0"> 
<div class="entry-image">googletag.cmd.push(function() { googletag.display('div-gpt-ad-1362706866260-0'); });</div></div>'), 

有或無腳本標記添加直線上升,從谷歌的javascript代碼我的javascript代碼我的HTML部分中根本不起作用,:(我將如何去解決這個問題並正確地插入我上面的谷歌的JavaScript代碼到我的現有代碼

更新目前代碼:

<script type="text/javascript"> 
$(document).ready(function() { 
var $window = $(window); 
var resized = false; 
var resized500 = false; 
var resized600 = false; 
var resized700 = false; 
var resized800 = false; 

var sadfdsf = true; 
var theDivs = { 
     "div1": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper" id="div-gpt-ad-1362706866260-0"><div class="entry-image"><script type="text/javascript">googletag.cmd.push(function() { googletag.display("div-gpt-ad-1362706866260-0"); });</script></div></div>'), 

     "div2": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper"></div></div>'), 

     "div3": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper"></div></div>'), 

     "div4": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper"></div></div>'), 

     "div5": makeSpecialBox("doublearticlewrapperadmp", '<div class="articlewrapper"></div></div>'), 

function makeSpecialBox(className, content) { 
    // Create a new special box with the class name and the content 
    var specialBox = $("<div />").addClass(className).html(content); 
    // Return the special box 
    return specialBox; 
} 

function removeSpecialBoxes() { 
    // Get the special boxes 
    var specialBoxes = $("#wrapper div").filter("[class^=doublearticlewrapperadmp]"); 
    // Remove the special boxes 
    specialBoxes.remove(); 
} 

function setNthPosition(theDiv, newPos) { 
    // Generate the special box 
    var theClone = theDivs["div" + theDiv].clone(); 
    // Get the nth-child 
    var nthChild = $("#wrapper > div:nth-of-type(" + newPos + ")"); 
    // Append the special box after the nth-child 
    nthChild.after(theClone); 
} 

function checkWidth() { 
    var windowsize = $window.width(); 
    if (windowsize >= 1442) { 
     //if the window is in between the sizes of 440 and 500px 
     //if(resized==false){ 
     // Remove special boxes 
     removeSpecialBoxes(); 
     // Reposition 
     setNthPosition(1, 9); 
     setNthPosition(2, 15); 
     setNthPosition(3, 29); 
     setNthPosition(4, 35); 
     resized = true; 
     // } 
    } 

    windowsize = $window.width(); 
    if (windowsize > 1278 && windowsize < 1441) { 
     //if the window is in between the sizes of 500 and 600px 
     // if(resized500==false){ 
     // Remove special boxes 
     removeSpecialBoxes(); 
     // Reposition 
     setNthPosition(1, 7); 
     setNthPosition(2, 16); 
     setNthPosition(3, 31); 
     setNthPosition(4, 40); 
     resized500 = true; 

     // } 
    } 

    if (windowsize < 1278) { 
     //if the window is in between the sizes of 600 and 700px 
     // if(resized600==false){ 
     // Remove special boxes 
     removeSpecialBoxes(); 
     // Reposition 
     setNthPosition(1, 5); 
     setNthPosition(2, 15); 
     setNthPosition(3, 29); 
     setNthPosition(4, 39); 
     resized600 = true; 

     //} 
    } 
} 

// Execute on load 
checkWidth(); 
// Bind event listener 
$(window).resize(checkWidth); 
}); 

'

回答

1

你忘了你的腳本標籤:

var theDivs = { 
    "div1": makeSpecialBox("doublearticlewrapperadmp", 
    '<div class="articlewrapper" id="div-gpt-ad-1362706866260-0"> 
    <div class="entry-image"><script type="text/javascript">googletag.cmd.push(function() { googletag.display("div-gpt-ad-1362706866260-0"); });</script></div></div>'), 

也知道你的單/雙引號的使用。有些人可能會或可能不會需要太逃逸(儘管它似乎你不會需要...),但我沒有改變周圍的單引號雙引號在googletag.display("div-gpt-ad-1362706866260-0")部分。

編輯:

你可以在這裏找到你的解決方案:Script tag in JavaScript string

var test = '...... </scr'+'ipt>......'; 

這小動作也許能繞過瀏覽器解析您的標記。

所以你的情況:?

var theDivs = { 
    "div1": makeSpecialBox("doublearticlewrapperadmp", 
    '<div class="articlewrapper" id="div-gpt-ad-1362706866260-0"> 
    <div class="entry-image"><script type="text/javascript">googletag.cmd.push(function() { googletag.display("div-gpt-ad-1362706866260-0"); });</scr' + 'ipt></div></div>'), 
+0

我編輯我的問題,這是行不通的,有或無:( – Dyck 2013-03-08 02:06:55

+0

你檢查你的報價腳本標記我也editted單引號你有你的裏面JavaScript的 – Jace 2013-03-08 02:07:30

+0

'的googletag.display( 'DIV-GPT-AD-1362706866260-0');' - >'的googletag.display( 「DIV-GPT-AD-1362706866260-0」);?' – Jace 2013-03-08 02:08:05