0

Is there any code that I could add to the header of every page that would insert HTML at 2 or 3 points on a page (probably in the body), spread out evenly? This would be amazing for the platform that I am currently working on.

For example, maybe you wanted to embed a video using this. Ideally, I could put the code in the header, and it'd insert it 2 or 3 times in the body -- spread evenly.

4
  • It's possible. How it's done would be quite specific to the HTML in question. Commented Jul 10, 2016 at 3:18
  • @ceejayoz what if we did it with AdSense code? Commented Jul 10, 2016 at 3:22
  • Doesn't matter what you're injecting. You're going to have to write some custom JS that you put in the header/footer you have access to. What it does will depend on the HTML structure of the pages you're trying to affect. Commented Jul 10, 2016 at 3:24
  • Oh sorry, I didn't get what you meant by that. Any suggestions for a page like izzofit.com? Commented Jul 10, 2016 at 3:26

2 Answers 2

1

Here you go: https://jsfiddle.net/westryder907/808xtyq7/

$(document).ready(function() {

  $('.body > div').each(function(i, el) {
    if (i % 3 === 0) {
      $(this).append(" Inserted text via jQuery 2.2");
      if (i > 3) {
        return false;
      }
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="header">
  <div class="body">
    <div class="region1">
      Region1
    </div>
    <div class="region2">
      Region2
    </div>
    <div class="region3">
      Region3
    </div>
    <div class="region4">
      Region4
    </div>
    <div class="region5">
      Region5
    </div>
    <div class="region6">
      Region6
    </div>
    <div class="region7">
      Region7
    </div>
    <div class="region8">
      Region8
    </div>
    <div class="region9">
      Region9
    </div>
    <div class="region10">
      Region10
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

8 Comments

This works very well, thank you. Is there any way to make it only do it 2 or 3 times, though? Also, any way to make it skip a div class or two, or space them out evenly? (reason being the structure of the page has many, many of the same div classes and I would need that for it to work perfectly)
This would actually be critical, then it would work absolutely perfectly!
Thanks for that. The issue is, they're all <div class="region1">...any ideas for how to deal with this?
Oh wait, never mind! It works perfectly! Thanks so much!
The jquery doesn't look at class names it looks at the element which is DIV, which they all are.. It iterates through them an on every odd one up to 3 it inserts new content. the class names are just there because I added them there so it wouldn't look so repetitive.
|
0

Why not just edit your code and manually insert content from an external file with PHP?

I think more information is needed to fully understand why you'd want to approach this with your current method. It just doesn't make much sense to me why you would want to do it like this?

1 Comment

Sorry for not including more information -- I'm currently investigating using a platform that is too limiting for me to do that. For example, I could go in and modify every single page and add the HTML, but I would have to do that page by page, post by post. I can't edit the code behind everything that's on the front end. But, I can insert code into the headers and footers, which is why I'm exploring this solution. Short: I can't edit CSS and JS and can only edit pages individually -- not like template files -- but can add code to the header and footer for the entire website. Platform = limit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.