Tell me please. It is necessary for the script to pass an id, which will be written in the form:

<script type="text/javascript" src="скрипт.js?id=17"></script> 

what would then substitute this id in the URL address that is formed by the java script

The bottom line is that the site has an affiliate program. To track the movement of the banner, the partner id (affiliate) is transmitted to the url.

the whole script is already ready, but I can’t capture the id at the address (((

  • Can alert (id); will display id? - Rules

3 answers 3

If the site is in PHP, then:

  • in .htaccess:

    AddHandler application/x-httpd-php .php .js

  • in script.js:

    <?php $id = intval($_GET['id']); // Π’ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π΄Ρ€ΡƒΠ³ΠΈΠ΅ дСйствия ?> ... client.id = <?=$id ?> ...

You can also use SSI :

  • in .htaccess:

    AddHandler server-parsed .shtml .js

  • in script.js:

    ... client.id = <!--#echo var="QUERY_STRING"--> ...

  • On the page:

<script type="text/javascript" src="скрипт.js?17"></script>

Only this is the wrong approach. If your event is large enough, it would be better to give everyone the same script, and set id in the code on the page:

 <script> client = { id: 17, // Π’ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π΄Ρ€ΡƒΠ³ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ } </script> <script type="text/javascript" src="http://......./скрипт.js"></script> 
     var tmp = new Array(); var tmp2 = new Array(); var param = new Array(); var get = $('script[src*="скрипт.js"]').attr('src').replace(/[^\?]+\?/, ''); if(get != '') { tmp = (get.substr(1)).split('&'); for(var i=0; i < tmp.length; i++) { tmp2 = tmp[i].split('='); param[tmp2[0]] = tmp2[1]; // ΠΏΠ°Ρ€Ρ‹ ΠΊΠ»ΡŽΡ‡(имя ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ)->Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ } for (var key in param) { var id = key } } 

    Here it will work. the snag was in

     var get = $('script[src*="скрипт.js"]').attr('src').replace(/[^\?]+\?/, ''); 

      Getting the script without jQuery and with support for older browsers

       var scripts = document.getElementsByTagName('script'), len = scripts.length, re = /скрипт\.js$/, src, myScriptSrc; while (len--) { src = scripts[len].src; if (src && src.match(re)) { myScriptSrc = src; break; } }