0

I am setting up a script/div pair that can be posted on an EXTERNAL site so someone can view the content from MY site. It works fine where there is only ONE script/div pair on the page but when there are multiple instances, the parameters from the LAST instance are the parameters every instance uses.

For example I have:

<div id="1"></div>
<script src="script.js?id=1"></script>

<div id="2"></div>
<script src="script.js?id=2"></script>

<div id="3"></div>
<script src="script.js?id=3"></script>

But every instance of script.js gets the id "3". I'm using this bit of code to get the script URL:

var scripts = document.getElementsByTagName('script');
var thisScriptElement = scripts[scripts.length - 1];
var scriptPath = thisScriptElement.src;

It's clear the second line is the problem, but I have no idea how to fix this issue.

user2803250
  • 111
  • 1
  • 1
  • 4
  • You should provide us with more explanation and code – Wezelkrozum Jun 23 '14 at 00:43
  • That's all there is to it. I'm calling the script multiple times and the specific line of code that reads "var thisScriptElement = scripts[scripts.length - 1];" is returning the last instance of the script every time. – user2803250 Jun 23 '14 at 00:46
  • 1
    This might help you: http://stackoverflow.com/questions/2190801/passing-parameters-to-javascript-files – hlscalon Jun 23 '14 at 00:47
  • That is logical. Since scripts contains the 3 script Dom elements, and scripts.length equals 3. That minus 1 equals 2, which is in scripts[scripts.length] the 3rd script Dom element, which has ?Id=3 as url – Wezelkrozum Jun 23 '14 at 00:50
  • The first answer to that adds another line of code and I'm trying to keep it small. The second answer is doing what I'm doing now and it's not working. – user2803250 Jun 23 '14 at 00:52
  • Wez I thought the logic behind it was that each script runs one after the other so the current script being called is always the last one? You're definitely right though, so how can I fix this? – user2803250 Jun 23 '14 at 00:53
  • The only reliable way I see would be to use a serverside script(which may retrieve the REQUEST_URI and creates the variable/prints the content) instead of static js-file. – Dr.Molle Jun 23 '14 at 00:56
  • Wez I just added this line "$(scripts).each(function (key) {alert (key.src);});" under the first line, and it is still showing each id as "3". So I think the browser is somehow replacing each instance of the script with the last one, I have no idea. – user2803250 Jun 23 '14 at 00:57
  • Molle how would I do that? Change the php.ini to have php run on .js files then echo it into a variable? – user2803250 Jun 23 '14 at 00:59
  • There is no need to give it the extension .js , you may also use a *.php and send an appropriate Content-Type-header. The variable may be "created"(printed) via PHP based on `SERVER['REQUEST_URI']`. – Dr.Molle Jun 23 '14 at 01:03

1 Answers1

0

There is no reason to download the script 3 different times if it isn't changing at all. I would recommend you get the script once, and make the script's contents into a fucntion you can run multiple times.

<script src="script.js"></script>

<div id="1"></div>

<div id="2"></div>

<div id="3"></div>

<script>
window.onload = function(){
    scriptsFunction(1);
    scriptsFunction(2);
    scriptsFunction(3);
}
</script>

script.js:

scriptsFunction = function(id){
    //All of the current script's content
};
Adam Merrifield
  • 10,307
  • 4
  • 41
  • 58