Adding JS Code Inside Area (c5.6.3.2)

Permalink
Hi All:

Is there a way to dynamically add inline JS only AFTER the first closing p tag within an area content block?

Note: This content block is used in numerous pages and instead of adding JS code one-by-one to each page, I'd like to add it to the page template like "right-sidebar.php" somehow.

Please help!

Thanks,
Keith

MavenMedia
 
PineCreativeLabs replied on at Permalink Reply
PineCreativeLabs
The easiest way to do this would be to add the embedded javascript via the HTML block.

To add it to a page type:
1. In the dashboard, go to "Page Types".
2. Click the "Defaults" button for the page type you want to edit.
3. In edit mode, use the HTML block to paste your code in.
4. Click the block and select "Setup on Child Page" from the menu. Check all pages that you want to apply the changes to. Be sure to save and publish.

Hope that helps.
MavenMedia replied on at Permalink Reply
MavenMedia
I understand what you are trying to do, but what I meant was to add a few
lines of JavaScript inside the area after the first paragraph only.

Every area in this website has several paragraphs of text.

Do you think this is possible?
On Jan 31, 2015 9:05 PM, "concrete5 Community" <discussions@concretecms.com>
wrote:
JohntheFish replied on at Permalink Reply
JohntheFish
Why is the location of the javascript on the page important?

In most cases, you can put javascript anywhere on a page and use selectors to decide which tag it works on.
MavenMedia replied on at Permalink Reply
MavenMedia
Hi John:

The location is important because the snippet of JS code is an Ad Placement from Google/Yahoo.

I want to test a different place of ad placement which is right under the 1st paragraph or right after the 2nd paragraph.

I figured this might not be easy with concrete5 code but I wanted to ask anyway.

I think the best option is to do a JS .append() though it's not working probably because the code is conflicting with the inline JS:

<script type="text/javascript">
   $('#article-single .prl-entry-content p:nth-child(2)').append("<center><style>.responsive-ad-7 { width: 300px; height: 250px; display: none !important; }@media(max-width: 768px) { .responsive-ad-7 { width: 300px; height: 250px; display: block !important; } }</style><ins class='adsbygoogle responsive-ad-7' style='display:inline-block' data-ad-client='ca-pub-' data-ad-slot='' data-ad-format='auto'></ins></center><script>(adsbygoogle = window.adsbygoogle || []).push({})</script>");
</script>
MavenMedia replied on at Permalink Reply
MavenMedia
Nevermind, this code is working. I just didn't properly format the last few tags of code because they were escaping the JS:

$('#article-single .prl-entry-content p:nth-child(2)').append("<div class='space-top space-bot prl-mobile2' style='margin-left:0;'><center><style>.responsive-ad-7 { display: none !important; }@media(max-width: 959px) { .responsive-ad-7 { width: 300px; height: 250px; display: block !important; } }</style><ins class='adsbygoogle responsive-ad-7' style='display:inline-block' data-ad-client='ca-pub-XXX' data-ad-slot='XXX' data-ad-format='auto'></ins></center><script>(adsbygoogle = window.adsbygoogle || []).push({})<\/script><\/div>");
JohntheFish replied on at Permalink Reply
JohntheFish
Looks like a good solution. You could probably put it in a ready handler and attach it to the local or global footer area (in an HTML block as @growthcurve suggested) or in the footer of the theme.