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?

3
  • 1
    stackoverflow.com/questions/3021737/… Commented Sep 10, 2013 at 7: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" Commented Sep 10, 2013 at 8:03
  • @SelvamPalanimalai - please make your comment an answer. Then I can give it a big green tick! Commented Sep 10, 2013 at 9:05

1 Answer 1

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".
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.