1

This is with reference to the question : How may I reference the script tag that loaded the currently-executing script?

And the most upvoted answer. It mentions in option 6 : "6. Get the last executed script"

Problems

Does not work with asynchronous scripts (defer & async)
Does not work with scripts inserted dynamically

My script is not async, but the page might have other scripts that are async or defer. Would this option work in that case as well? I have not been able to find an answer to this anywhere.

Community
  • 1
  • 1
ZeroG
  • 43
  • 4
  • Not necessarily, no. If you don't need IE support, check out: https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript – Scott 'scm6079' Jun 12 '15 at 05:59
  • @Scott'scm6079' : document.currentScript is definitely the cleanest solution and I would've gone for it, but unfortunately, I do need IE support. – ZeroG Jun 12 '15 at 06:08
  • @humble.rumble : For some reason, I don't want to assign an id to the script tag. So that is not an option. – ZeroG Jun 12 '15 at 06:37
  • Note that *currentScript* only tells you the script that is executing if it's called while the script is loading. Once the load event has occurred, there is no *currentScript*. Why do you need to know? What problem are you trying to solve? – RobG Jun 12 '15 at 06:45
  • Not sure the details of your needs - but since you are putting a script on a remote site, you could have the script you distribute do nothing but dynamically create a new script node, which you can have a handle to. Depending on why you need to know the script tag, that can help... I've got lots of tricks, but need to know a little more about what you are trying to accomplish. – Scott 'scm6079' Jun 12 '15 at 07:07
  • @humble.rumble : I might have to include the script at multiple places in the target webpage. It is somewhat like an ad tag. I want to insert the ad at the node where my script resides. I don't want to create a new id for each place since I am not putting the script tag in the page (the page owner would be supposed to do that and I don't want to rely on him to generate a new id, etc). – ZeroG Jun 12 '15 at 07:55
  • @humble.rumble : Thanks. The only doubt I had was in the case when the page might have other script tags which could be asynchronous. Would the solution work in that case as well? – ZeroG Jun 12 '15 at 08:16

1 Answers1

-1

If I understand you problem, I think closures would help solve this.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

You may also want to organize your JS files so that your async files are included in an async.js file and non-acyned files in non_async.js and so on.

I hope this is clear.

Cheers,

Tim

WebRuin
  • 127
  • 1
  • 3
  • 10
  • Tim: The problem is that I don't own the website. I am just giving out a small script to the site owner to be included in his/her page. So I have no control over everything else that the site owner has. – ZeroG Jun 12 '15 at 06:07
  • I see. Then the closure like is for you. – WebRuin Jun 12 '15 at 06:13
  • Closures are an artefact of javascript's lexical scoping, they have nothing to do with linking a particular execution context to the script element that created it. – RobG Jun 12 '15 at 06:48