0

I have two or more widgets in one page with a remote call. For example:

<script async src="https://example.com/widget.js?cid=2" type="text/javascript"></script>
<div id="cid_2"></div>

<script async src="https://example.com/widget.js?cid=3" type="text/javascript"></script>
<div id="cid_3"></div>

Inside each widget.js I need to take the cid parameter, do some tasks inside the widget that need the cid parameter and write the result on the corresponding <div> (#cid_2, #cid_3 etc).

How do I get the cid value from the url (.js?cid=) from inside widget.js?

Matheus Avellar
  • 1,507
  • 1
  • 22
  • 29
DaveIt
  • 799
  • 1
  • 8
  • 15
  • 2
    Possible duplicate of [How to get the value from the GET parameters?](http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters) – LellisMoon Mar 06 '17 at 15:54
  • No duplicate please read my example is not the same. – DaveIt Mar 06 '17 at 15:59
  • Maybe I didn't get something, but my first impression is very negative: this is loading the same javascript file several times, one for each widget. Terrible. Load the JS once, and then add `script` tags to call a function for each widget. May I also suggest Google Analytics code as a clever way to embed JS with great flexibility: https://developers.google.com/analytics/devguides/collection/analyticsjs/ – J. Bruni Mar 07 '17 at 19:10

2 Answers2

0

i have found a way to do this getting from script src and decoding with a function. this is the example:

//Function to get parameters from script src
function getSRCparameter(sParam) {
    var scripts = document.getElementsByTagName('script');
    var index = scripts.length - 1;
    var myScript = scripts[index];
    // myScript now contains our script object
    var queryString = myScript.src.replace(/^[^\?]+\??/,'');

    var sPageURL = queryString,
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
}

//Example code
$(document).ready(function(){

    //parameter in the src
    var getValue = getSRCparameter("cid"); 

    //test results in console
    console.log("my get value is: "+getValue); 

    //put the info inside the divs
    $("#cid_"+getValue).html("ID Get is:"+getValue);
});
TriForce
  • 415
  • 2
  • 8
  • But OP wants to fetch the query string from inside the `.js` file. Getting the `src` for the ` – Matheus Avellar Mar 06 '17 at 18:05
  • thats is an example to get the parameter inside his own JS (widget.js) so he need to add the function inside... i already tested creating a empty widget.js – TriForce Mar 06 '17 at 18:09
  • Oh yeah, I suppose since it'll be added to the DOM, it should work. My bad. – Matheus Avellar Mar 06 '17 at 18:10
  • mhhh m8 thx all but i need to fetch inside my widget.js suppos that i have severla scripts in destination web side i need a method to address exactly my script – DaveIt Mar 07 '17 at 09:13
0

Read from window.location.href after the question mark, split by "&" and find the parameter whose part before the "=" matches your criteria.

var parameters = window.location.href.split("?")[1].split["&"];
for (var index in parameters) {
    var param = parameters[index].split("=");
    if (param[0] === "cid") {
        //Do something
    }
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175