Get the current script tag
I think you could safely do this, and not in a DOM-ready mode.
var scripts = document.getElementsByTagName("script"),
selfScript = scripts[scripts.length-1];
I would suggest that if you can, put any configurations in another script block.
<script>
window.attr1 = 'val1';
window.attr2 = 'val2';
window.attr3 = 'val3';
</script>
<script src="x.js"></script>
jQuery Select current script tag
Outside the document ready method, just do $('script').last();
:
<script type="text/javascript">
var currentScript = $('script').last();
$(document).ready( function(){
//Use the variable currentScript here
})
</script>
Or simply give an id to your script.
Get DOM element where script tag is
At the time the script is run, the document is only partially loaded. So your script element would be the last node in the document:
var target = document.documentElement; // start at the root element
while (target.childNodes.length && target.lastChild.nodeType == 1) { // find last HTMLElement child node
target = target.lastChild;
}
// target is now the script element
alert(target.parentNode); // this is p
How do I get a parent element of script tag in javascript?
In the script test.js
you can use:
scriptParent = document.currentScript.parentElement;
or you can use other solutions from How may I reference the script tag that loaded the currently-executing script?
and add .parentElement
to it.
Related Topics
JavaScript Object: Access Variable Property by Name as String
When Is JavaScript'S Eval() Not Evil
How to Use Multiple Versions of Jquery on the Same Page
How to Access Object Properties Containing Special Characters
Does JavaScript Pass by Reference
Can a Website Detect When You Are Using Selenium With Chromedriver
Ecmascript 6 Arrow Function That Returns an Object
How to Make the First Letter of a String Uppercase in JavaScript
What Characters Are Valid For JavaScript Variable Names
Is There an "Exists" Function For Jquery
How to Select Text Nodes With Jquery
What Does the Exclamation Mark Do Before the Function
Why Can't I Directly Modify a Component'S State, Really
Why Is the Method Executed Immediately When I Use Settimeout
Wait Until All Jquery Ajax Requests Are Done