Put the script to the end of the BODY tags using jQuery
This is the script I want before the body tag:
<script type="text/javascript"> var vglnk = { api_url: '//api.viglink.com/api', key: '89dcd0a12ff35d227eaaaff82503030b' }; (function(d, t) { var s = d.createElement(t); s.type = 'text/javascript'; s.async = true; s.src = ('https:' == document.location.protocol ? vglnk.api_url : '//cdn.viglink.com/api') + '/vglnk.js'; var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script')); </script> I want this code to be where I put "HERE"
<html> <head> </head> <body> some html and stuff HERE </body> </html> How do I do this in jQuery? (I do this with the extension. Mostly in Chrome, but also FF and IE.)
Any help is appreciated.
You need a content script to make an insert on every page you want.
The script content code is really simple and does not need jquery.
var code = "your script code here"; var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.appendChild(document.createTextNode(code)); document.body.appendChild(script); As soon as it will be called only when you do not even need to define a function. You can debug the code using the debugger on any website containing a script (F12), you will see your code in the script tab.
I had the same problem with the best place to add jQuery: in the header or before the body tag? The answer is that it does not matter.
The entire page (or DOM) must be initialized or loaded in order to do what you are doing. ALSO .. The more information in the body, the more it depends on the need to download the document.
The 2 sentences above are redundant because:
All jQuery ui / basic syntax / widgets etc. run with: $(document).ready( function() { $("#some_id").click( function { MORE CODE HERE }); });
The above code means that before jQuery can be launched, you need to download the full html page (or "document") and also initialize.
There is jQuery loading order for ui to work. The actual library should be first, then ui. The library can be downloaded from jquery.com and uploaded to designers' web space or via CDN (CONTENT CONTENT CONTENT) to save on bandwidth. Here is an example of how the order should be:
<script src="http://code.jquery.com/jquery-1.6.4.min.js" ></script> <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js" ></script>
Note that the library is the first line, then the user interface, in which case I downloaded jquery mobile.
In conclusion, it does not matter, preference is mainly. More details here https://forum.jquery.com/topic/unclear-where-document-ready-goes#14737000004139019