0

I am trying to display some banner from a remote location using hrefs and img in JavaScript.

On client side:

<script src="data/JScript.js?id=123" type="text/javascript"></script>

I want some solution so that I can make 123 dynamic in below JS file. As of now it is hard coded.

I just want that i will give client the above JS link and it will generate a dynamic link to a website with a querystring paramenter like (www.abc.com/123)

JS file (JScript.js):

document.write('<div style="text-align:center" class="img-responsive" ><a href="www.test.com/123" > <img style="width:200px;height:200px;" src="http://pixel.nymag.com/imgs/daily/intelligencer/2015/07/18/20-donald-trump.w529.h352.2x.jpg" alt="" > </img> </a> </div>');
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
techsolver
  • 103
  • 1
  • 1
  • 7
  • 2
    looks like you need this http://feather.elektrum.org/book/src.html – atinder Jul 24 '15 at 06:03
  • The link provided by @atinder is no longer necessary. See [this](http://stackoverflow.com/questions/403967/how-may-i-reference-the-script-tag-that-loaded-the-currently-executing-script) answer for several better options – Nik Klassen Jul 24 '15 at 06:08

1 Answers1

0

When your script runs you can search for your script tag on the page then parse the query string

var el = document.querySelector('script[src^="http://example.com/path/to/script.js"]');
var num = /\?id=(\d+)/.exec(el.src)[1];
document.write('<div style="text-align:center" class="img-responsive" ><a href="www.test.com/'+num+'" > <img style="width:200px;height:200px;" src="http://pixel.nymag.com/imgs/daily/intelligencer/2015/07/18/20-donald-trump.w529.h352.2x.jpg" alt="" > </img> </a> </div>');
Adam Heath
  • 4,703
  • 2
  • 35
  • 50