2013-03-08 7 views
0

Я пытаюсь вставить этот код Javascript через. Google:Вставка 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>'), 

Добавления прямо яваскрипт кода от Google в моем HTML части моего яваскрипта коды просто не работает, с или без тегов сценария :(Как я могу обойти эту проблему вопрос и правильно вставить мой выше Google яваскрипта код в мой существующий код

Обновленный Текущий код:

<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").

EDIT:

Вы могли бы найти решение здесь: 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

+0

ли вы проверить ваши котировки Я также Editted одиночных кавычек вы имели внутри ваш javascript – Jace

+0

'googletag.display ('div-gpt-ad-1362706866260-0');' -> 'googletag.display (" div-gpt-ad-1362706866260-0 ");' – Jace

Смежные вопросы