0

I've been using the following dynamic scripting technique for executing a site-specific subroutine in an otherwise generic script, viz

siteExtras : function (cid, extras) {
  var script = document.createElement("script");
  script.src = 'https://' + cid + '.mysite.com/' + cid + "_extras.js?" + extras;
  script.type = 'text/javascript';
  var h = document.getElementsByTagName("script")[0];
  h.parentNode.insertBefore(script, h);
},

So given the following invocation

this.siteExtras('cycles','concept=tandem');

script.src thus becomes

https://cycles.mysite.com/cycles_extras.js?concept=tandem

where cycles is a client-specific subdomain I've created on mysite.com.

The issue I'm having is that I've been assuming that cycles_extras.js would be capable of reading it's own search string, using

//cycles_extras.js
function getParameterByName(name) {
    var args = location.search.substr(1).split('&');
    for (var i = 0; i < args.length; i++) {
        var parts = args[i].split('=');
        if (name === parts[0]) {
            return unescape(parts[1]);
        }
    }
    return "";
}
var concept = getParameterByName('concept');
...

however, when cycles_extras.js is executed, it takes its location.search from the browser, not from the https://cycles.mysite.com/cycles_extras.js?concept=tandem URL.

Am I attempting the impossible here? Or is something missing?

Access Denied
  • 224
  • 1
  • 14
bugmagnet
  • 7,631
  • 8
  • 69
  • 131
  • 1
    http://stackoverflow.com/questions/3021737/passing-querystring-style-parameters-into-javascript-file – gvee Sep 10 '13 at 07:51
  • 1
    you can use global namespace for help. Bind the values to a variable and attach it to window or document objects. Example, document.concept = "tandem" or window.concept = "tandem" – Selvam Palanimalai Sep 10 '13 at 08:03
  • @SelvamPalanimalai - please make your comment an answer. Then I can give it a big green tick! – bugmagnet Sep 10 '13 at 09:05

1 Answers1

1

You can use global namespace for help. Bind the values to a variable and attach it to window or document objects. Example,

document.concept = "tandem" 

window.concept = "tandem".
Selvam Palanimalai
  • 1,550
  • 1
  • 10
  • 13