4

I am working with a system where I cannot directly edit the HTML on each page. I want to add the top of the body of a particular page but the box where I enter the HTML automatically places the script just above

I tried this:

<script>
$('body').prepend('<script src="myscript.js"></script>');
</script>

Any help would be greatly appreciated, Thank you!

Jenny
  • 781
  • 1
  • 9
  • 24
  • Why not simply do: `$.getScript('myscript.js');`? Docs: https://api.jquery.com/jQuery.getScript/ – gen_Eric Mar 25 '14 at 16:58
  • 1
    Why do you think you need to add the script to just above the body tag? – Adam Jenkins Mar 25 '14 at 17:00
  • [This is better](http://stackoverflow.com/a/8278513/1267304) than any jQuery solution. Check it out. – DontVoteMeDown Mar 25 '14 at 17:00
  • I am working with a client who is working with a third party that requires their conversion script placed at the top of the body. – Jenny Mar 25 '14 at 17:04
  • 1
    @Jenny: Does it actually matter where the script is placed? Or are they just telling you that? Have you tried putting the script elsewhere (by just doing `` in the HTML) to see if it works? Also, where are you running your script? – gen_Eric Mar 25 '14 at 17:06
  • 2
    @Jenny - why do they require it place there? To be technical about it, if you are appending the `script` tag to the page with JavaScript, then the entire reason of placing the `script` tag before the `body` tag has already been defeated. – Adam Jenkins Mar 25 '14 at 17:11
  • The script is not working placed at the end of the body, the third party said they require that it is placed at the top of the body. I would like to place it at the top of the body like they requested so that if it is still not working they will troubleshoot it. – Jenny Mar 25 '14 at 17:37
  • 1
    @Jenny: If their script only works at the top of the body, then that means they are doing something wrong. – gen_Eric Mar 25 '14 at 17:39
  • Was anyone able to come up with anything to place the script just below ? – Jenny Mar 25 '14 at 17:41
  • Where in the page are your script running? What part of the page can you edit? Why can't you directly edit the HTML? – gen_Eric Mar 25 '14 at 17:45
  • Unfortunately, I don't get to discuss this with them I just have to get it to the top of the body. The software doesn't allow it. I have access to the homepage body the content on the other pages is systematically generated. I have access to the head and another box for the footer that adds info just above the closing body tag. – Jenny Mar 25 '14 at 17:49
  • It's ok if this is not possible, I can let the client know. Just wanted to give it my best shot so I tried it then reached out to you kind folks but totally understand if this is not do-able. – Jenny Mar 25 '14 at 17:55
  • It might not be possible. If you do it in the `` then the `` doesn't exist in the DOM yet, and if you do it before/after the `` then it's too late to "prepend" the script. – gen_Eric Mar 25 '14 at 18:01
  • Is there a way to prepend it on the first div, so it would appear above the first div which would put it just below the body? I tried this but it didnt work: '); – Jenny Mar 25 '14 at 18:02
  • I guess it has to do with the script tags being within the statement huh? – Jenny Mar 25 '14 at 18:04
  • @Jenny: What *exactly* does the script do? *Why* does it need to be right after the ``? My guess is that is uses `document.write()`, which only works *before* the page is fully loaded. Once your script runs, the page is already loaded and `document.write()` won't work correctly any more. Like I said, "If their script only works at the top of the body, then that means they are doing something wrong.". – gen_Eric Mar 25 '14 at 18:13
  • It is a conversion script for tracking, it does have the document.write, I get that their script may be incorrect but I need to get it placed in there for the client and it has to go just below the body. Is it possible to tell the script to go just below the body? – Jenny Mar 25 '14 at 18:25

1 Answers1

0

I don't understand why you are doing this, but it is possible... :)

If you must insert javascript code at the begining of the tag body, you can load your javascript file like a "jsonp ajax call" and inside it call to 'prepend' jquery function.

Example:

<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <h1>Test</h1>
    <p>Test</p>
    <script type="text/javascript" src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
    <script type="text/javascript">
      $( document ).ready( function() {
        $.ajax({
          dataType: "jsonp",
          url: "myscript.js"
        })
        .done( function( d, response ) {
          console.log( "JS LOADED" );
        });
      });
    </script>
  </body>
</html>

And in your "myscript.js" file something like:

$( "body" ).prepend( "<script>" +
                     "var a = 1;" +
                     "console.log( a );" + 
                     "/* AND ALL THE JS CODE TO INSERT " +
                     "AT THE BEGINING OF TAG BODY */</script>" );

Don't forget with the semicolon (;) at the end of every js line.

manufosela
  • 469
  • 6
  • 8