I want to access current script or shadow root from inside of shadowDOM.
And, final purpose is getting div.target
element in same shadowDOM.
I try using document.currentScript
.
However, it returns null
.
Therefore, I cannot detect where shadowDOM I am in.
Is there a way to do it?
<template id="template">
<div class="target"></div>
<script>
// How to access self script element or above element which has target class?
console.log(document.currentScript)
// null
</script>
</template>
<div class="sr"></div>
<script>
document.querySelector(".sr").attachShadow({mode: "open"}).append(
document.querySelector("#template").content.cloneNode(true)
)
</script>