Help with this code

Help with this code

James

A Verified Legend
Gold Member
Local time
01:57
Joined
Oct 13, 2019
Messages
49

Hey

I'm trying to fire this script, but it's not liking the closing </script> it seems to be looking at the closing </script> in prdom.query. How would I make this work correctly?

Cheers

JavaScript:
<script class="pb">

function DoSomething(e){

    e.data.forEach( ele => {

      if (ele.id == '1113069') {

      prdom.query('#belowimage').html('<div align="center" id="chasethetrend_320x50_quiz_sticky"><script data-cfasync="false" type="text/javascript">freestar.queue.push(function(){googletag.display("chasethetrend_320x50_quiz_sticky");});</script>');

        console.log('NTV-NoAd Executed');

    }})

}

var ntvSubscribe = function(){

    ntv.Events.PubSub.subscribe("noad", DoSomething)

}

var ntvLoad = function() {

    if (window.ntv) {

        ntvSubscribe();

    } else {

        setTimeout(ntvLoad, 100);

    }

}

ntvLoad();

</script>

 
Last edited:

Adam

Mr. Webwide
Administrator
Local time
01:57
Joined
Sep 24, 2019
Messages
1,248
Pronouns
he/him

Are you getting an error message in the JavaScript console? If so can you share here?

F12 to open developer tools then click console. There will be a red box if there is an error.

 

Adam

Mr. Webwide
Administrator
Local time
01:57
Joined
Sep 24, 2019
Messages
1,248
Pronouns
he/him

Oh, ha, I see the problem. It is this line here.

Code:
prdom.query('#belowimage').html('<div align="center" id="chasethetrend_320x50_quiz_sticky"><script data-cfasync="false" type="text/javascript">freestar.queue.push(function(){googletag.display("chasethetrend_320x50_quiz_sticky");});</script>');
The HTML is escaping from your string. As I understand it, loading script tags using html(), innerHTML or any of those methods is blocked for security reasons so this could be why. There are ways around it but not quite sure what is best. Will get back to you on that one someone else might have good solution in mean time.

 
Last edited:

Adam

Mr. Webwide
Administrator
Local time
01:57
Joined
Sep 24, 2019
Messages
1,248
Pronouns
he/him

Try changing that problem line to the following...

JavaScript:
// Add the div as before
prdom.query('#belowimage').html('<div align="center" id="chasethetrend_320x50_quiz_sticky">');
// Create a new script element
var newScript = document.createElement("script");
// Add our dynamic script as a text node
var inlineScript = document.createTextNode("freestar.queue.push(function(){googletag.display('chasethetrend_320x50_quiz_sticky');});");
// Add dynamic script to the new script element
newScript.appendChild(inlineScript);
// Add it back inside the quiz_sticky div
document.getElementbyId('chasethetrend_320x50_quiz_sticky').appendChild(newScript);

It might run in to problems if it needs to run multiple times per page, does it?

 
Last edited:

Michael

😎
Local time
01:57
Joined
Oct 5, 2019
Messages
312

You have to split the </script> tag into 2 parts, otherwise you get the issue where it does indeed get parsed.

Use:

Code:
      prdom.query('#belowimage').html('<div align="center" id="chasethetrend_320x50_quiz_sticky"><script data-cfasync="false" type="text/javascript">freestar.queue.push(function(){googletag.display("chasethetrend_320x50_quiz_sticky");});</scr' + 'ipt>');

 

henrygarrett

New member
Local time
20:57
Joined
Oct 17, 2019
Messages
16

Also, I think the html string you are inserting doesn't have a closing </div>. It starts with a div, but that never gets closed. Maybe that would help.

 

James

A Verified Legend
Gold Member
Local time
01:57
Joined
Oct 13, 2019
Messages
49

You have to split the </script> tag into 2 parts, otherwise you get the issue where it does indeed get parsed.

Use:

Code:
      prdom.query('#belowimage').html('<div align="center" id="chasethetrend_320x50_quiz_sticky"><script data-cfasync="false" type="text/javascript">freestar.queue.push(function(){googletag.display("chasethetrend_320x50_quiz_sticky");});</scr' + 'ipt>');

Hi Michael, why is it that <script>'s still won't render, I can get text to fire correctly (example):

JavaScript:
prdom.query('#belowimage').html('test');

But if I try and fire a script within it, it doesn't fire:

JavaScript:
prdom.query('#belowimage').html('<div align="center" id="chasethetrend_320x50_quiz_sticky"><script data-cfasync="false" type="text/javascript">freestar.queue.push(function(){googletag.display("chasethetrend_320x50_quiz_sticky");});
</scr' + 'ipt>');

Is there a potential reason why calling a script this way wouldn't work?

Thanks!

 
Last edited:

Adam

Mr. Webwide
Administrator
Local time
01:57
Joined
Sep 24, 2019
Messages
1,248
Pronouns
he/him

Is that new line in the code before your closing script on here the same on your actual site? If so I think the line break might break it try it all on one line.

 
Top