0

I have a situation where I need to dynamically insert a block of code directly after an existing

<script src="......."></script> tag.

I am not able to make changes to the section in the site where the tag exists but need some javascript to load directly after that tag and before the next block of code executes.

Is this possible with jquery?

Thanks.

gteh
  • 741
  • 10
  • 17
  • Not really. You can insert a ` – Jonathan Lonowski Jan 14 '14 at 14:49
  • that's what I was afraid of. Will have to find another way then since we just can't modify that part of the site. – gteh Jan 14 '14 at 15:28

1 Answers1

0

Yes, you can select a script element in jQuery just like any other DOM element.

You can then use jQuery's .after() function to insert some content after the script closing tag

$(document).ready(function(){
    $('script').after('Your added content goes here.')
});
Xevi Pujol
  • 557
  • 3
  • 14
  • Thanks. Can I specify which script tag to target based on it's src? Will this actually execute in that order? or will it execute after everything has loaded. The problem is that I need this new block of code to execute after the tag,but before what follows. – gteh Jan 14 '14 at 15:25
  • You can select (http://api.jquery.com/category/selectors/) by id or class if your `script` tag has any. Or you could select by `src` value `$('script[src="...."]')`, though it's not very elegant nor robust. Overall, however, it looks like an awkward situation, are you sure you can't come up with another work-around? – Xevi Pujol Jan 14 '14 at 15:48