Search code examples
javascripthtmltoken

Detect "value" by imported JS


I'm working on website where users can add projects. They can add a link of their website to project. But to verify that website is their they must add some script and generated token to head of their website index.

I want to make something like this:

<script src="linktoscript" data-sitekey="token"></script>

But I don't know how to load that "token" at the script.

I tried:

var token = response('data-sitekey');

Didn't work.


Solution

  • Probably document.currentScript is what you are looking for. document.currentScript returns the DOM of <script>.

    Try something like this:

    console.log(document.currentScript.dataset.sitekey)
    

    I hosted a demo. Use "View Source" to see the code.