Ensure script tag is included as first tag in the body?

Brad Parks

I have a script file that I want third party implementors to include in their web pages.

I want this script to be the first tag included in the body, so

<body>
<script type="text/javascript" src="/my_script.js"></script>
...

What's the best way to go about doing this without using any third party libraries?

My intent is that this script gets run before any of the page is rendered. If the tag isn't included correctly, I want to display an error message to the developer, telling them to put the tag as the first tag in the body.

EDIT: This script is going to be used to protect against clickjacking, and will basically hide the entire document if the embedder isn't in a whitelist. Why does it need to be the first script in the page? To ensure that nothing clickable becomes visible before the script is run, and to ensure that other script errors don't interfere with this being run as well. So it doesn't absolutely need to be first, but it should be super close to first ;-) This approach is based off of this suggestions at the Owasp site, but is trying to be a more of a "just drop it in" solution.

Downgoat

First element in body

In /my_script.js. You can add this code:

if (document.body.firstElementChild == document.currentScript) {
    // Is first element
} else {
    // Is not first element
}

I'm not sure why you'd need to do that but here's how to do that.

Move script to first element

If you want to move the script:

document.body.insertBefore(document.currentScript, document.body.firstChid);
document.currentScript.parentElement.removeChild(document.currentScript);

Check if script is first script

if (( document.scripts || document.getElementById('script') )[0] == document.currentScript)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why are the Angular files included in the <body> tag?

Is it wrong to place the <script> tag after the </body> tag?

JSDOM is not loading JavaScript included with <script> tag

How to integrate Script tag into a body tag at end in an SPFX

why script tag after body tag counts in childNodes

MSL Socket Script displaying html and body tag

HTML5: Why a script tag needs to be placed at the end of body tag instead at the beginning of the body tag?

How to ensure a certain script tag remains at the top of <head>

How to add eternal script tag in Gatsby body ? With conditional rendering of script in body Tag?

Firefox extension: JavaScript file included via script tag is not working

Script tag within a script tag?

how to only target a css class if it is the first element within the body tag?

Load javascript file dynamically by appending script tag to body

Script tag works inside the body but doesn't work outside

Why does the handlebar script not work outside body tag

How to bypass filter if a tag is included?

CSS Body tag not working

How do I add a script tag before the closing body tag for an A/B experiment using Google Optimize?

Should I wait for DOMContentLoaded event if I place a script tag at the end of a body tag

Cannot read properties of null (reading 'innerHTML') Even With Script Tag After All Elements In My Body Tag

why script and div tag could much in html ,bug html and body tag only one

Make Div tag like body tag

removing span tag within body tag

Can a form tag enclose a body tag?

How <style> tag is working inside <body> tag?

difference between html tag and body tag

How to ensure that AMD dependency has been loaded from inside <script> tag code?

Vuex mapState reference error when vuex.js is included via script tag

JS file caching is different when included via script tag vs GET