0

I am trying to get host domain of an external script. For example:

<script src="//api.host.com/some.js">

Inside some.js:

console.log("Hi root domain, I am coming from " + document.referrer);

I want the some.js script to detect where it is hosted. Is this possible? Please help.

0

1 Answer 1

1

There isn't a built-in method of achieving this, but you can use:

var fileName = "some.js"
var hostedAt;
var scripts = document.getElementsByTagName("script");
for (var i = 0; i < scripts.length; i++) {
    let split = scripts[i].src.split(/\//g);
    if (split[split.length-1] === fileName) {
        split[split.length-1] = "";
        split = split.join("/");
        hostedAt = split;
        break;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's why I love programming than my GF. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.