0

I use a technology that needs to conduct different calls to the same external JS. The external JS is called inside an iframe and creates an output there. For example:

// Placement 1
<script async id="1234" src="external.js"></script>
// Placement 2    
<script async id="1235" src="external.js"></script>

I know there were various related questions, but they all assumed the external file is called just once, so it doesn't need to choose which ID to take parameters from.

Is there a way inside the external.js to know which ID called it and used its parameters? For example, use "this" or "that" depending on which ID called it:

<script async id="1234" src="external.js?use=this" ></script>
<script async id="1234" src="external.js?use=that" ></script>
// or
<script async id="1235" src="external.js" data-use="this"></script>
<script async id="1235" src="external.js" data-use="that"></script>
// etc. Even the MYLIBRARY way*
LWC
  • 1,084
  • 1
  • 10
  • 28
  • There is no way to achieve this, if you do not have access to that external script to begin with. – misorude Jan 11 '19 at 08:58
  • (“No way” might be too absolute; if the script was CORS-enabled, you could maybe fetch it via AJAX, and then evaluate it in a context where you provide some variable values upfront somehow.) – misorude Jan 11 '19 at 09:00

1 Answers1

0

What you are looking for is not consistent with JS behaviour. When you ask a browser to bring the same resource twice, (an image, a video or in this case a JS file), it will only bring it once. But why would you go like this ? Why not simply use a

function a(someId) {...}

and then send it 1234 or 1235 or whatever. much simpler I should think. BTW - I've never heard of data-use="this" or data-use="that". Is that some new JS langauge?

LongChalk
  • 783
  • 8
  • 13
  • As mentioned, it's a technology I use so I can't truly change and convert it. I just want to make some parameters there defined dynamically. I've just edited the question to reflect the external JS is called through an iframe which might explain your "will only bring it once" phrase. – LWC Jan 11 '19 at 08:53
  • The technologies and layers above JS are quite vast. JQuery, AngularJS, Backbone, React to name only a few. I don't know what technology you're working with. so ... I am sorry, I am not going to be able to help you here. good luck! – LongChalk Jan 21 '19 at 07:31