1

Hello i have this example

<script src="https://example.com/init.js?v=1589383111"></script>

How i get Argument v inside the init.js script

init.js script

var version = v; // argument from the src itself
Crustamet
  • 104
  • 1
  • 11
  • Could you provide some more detail about your problem? As it is, I'm not sure exactly what you're trying to achieve and how you've attempted to do it. – Run_Script May 13 '20 at 15:25
  • I use an asset minifier with CSP enabled that does not allow inline script and ii want to pass a variable that is already configured by PHP to the minified script. But i dont want to enable inline script – Crustamet May 13 '20 at 15:30
  • Duplicate: https://stackoverflow.com/questions/14904378/get-data-attribute-of-script-tag – isherwood May 13 '20 at 15:44

4 Answers4

4

You can access the current script tag with document.currentScript. Then its a simple matter of extracting the src attribute, and parsing it as a URL.

<!-- _src is used simply for demo purpose. You can't have a src attribute on a script tag with a body -->
<script _src="https://example.com/init.js?v=1589383111">
  const self = document.currentScript;
  const v = new URLSearchParams(
    new URL(self.getAttribute('_src')).search,
  ).get('v');
  
  console.log(v);
</script>
Olian04
  • 6,480
  • 2
  • 27
  • 54
  • nice, i understood the question differently but this is more likely what the OP was asking... – Mulan May 13 '20 at 15:40
1

I'd consider putting the version in a data attribute on the backend, along with an ID. Then you can access them from the DOM:

<script src="https://example.com/init.js?v=1589383111" 
    data-version="1589383111"
    id="importantScript"></script>

Now you can get the value of that attribute:

const version = document.getElementById('importantScript').getAttribute('data-version');
isherwood
  • 58,414
  • 16
  • 114
  • 157
1

From within the script:

const curScriptElement = document.currentScript;
const queryParams = new URLSearchParams(new URL(curScriptElement.src).search);
const version = queryParams.get('v');
console.log('version is', version);

See demo: https://plnkr.co/edit/vd02AYWHnJhY6eu1

In this case, mind the browser support (IE specifically):

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
-1

look at URL and URLSearchParams -

const u =
  new URL('https://example.com/init.js?v=1589383111')
  
const s =
  new URLSearchParams(u.search)
  
console.log(s.get("v"))
// 1589383111
Mulan
  • 129,518
  • 31
  • 228
  • 259
  • How would that URL value be available from the init function in the file? – isherwood May 13 '20 at 15:33
  • @isherwood if `https://example.com/init.js` is the homepage of the app... – Mulan May 13 '20 at 15:34
  • I don't understand. You're passing it as a magic string. Where does that come from? It's a dynamic value. – isherwood May 13 '20 at 15:35
  • isherwood, i was imagining the `init.js` as the entry point to a single-page app or something. in my mind, the question was more about getting the value out of the string, and less about how to get the actual url. there's a variety of ways to do that... – Mulan May 13 '20 at 15:39