View Full Version : change parameters of loaded script after page load?

09-06-2011, 05:30 PM
I doubt this is possible, but I have no idea what actually happens when a page loads, so I thought I would give it a shot...

if a js file that is loaded externally has parameters, is it possible to turn those parameters into variables, giving the user the option to select the variable's value after the page has loaded?

maybe a concrete example would be better...

in google maps, the api is loaded like this:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

but if you wanted to specify that you want the api to function in Spanish, you load it like this:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=es"></script>

and that language=es is the one that I want to change. But I can't seem to make it into a variable on page load, like this:

<script type="text/javascript">
var lang="es";
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=lang"></script>

much less make it changeable once the page has loaded.

I have a feeling that this is a really dumb question, but can it be done without reloading the entire page?

Logic Ali
09-06-2011, 07:20 PM
If the API doesn't have a method to change the language, you can pass a querystring parameter when you reload the page, whose value can be parsed into a variable. Then you generate the <script> tags using document.write, inserting the variable into the string at the appropriate point.

09-06-2011, 07:31 PM
yeah, I know I can do that, but what I'm asking is if I can do it *without* reloading the page...

Old Pedant
09-06-2011, 07:36 PM
In other words:

<script type="text/javascript">
var lang="es";
'<sc' + 'ript type="text/javascript" '
+ ' src="http://maps.google.com/maps/api/js?sensor=false&language=' + lang + '">'
+ '</sc' + 'ript>');

Seems hokey, but it works. You use your own JS code to write the request for the external JS code. You break up the <script> and </script> tags so they aren't misinterpreted as part of the outer code. (I think actually only the </script> tag needs to be broken up, but doesn't hurt to break up both.)

09-06-2011, 07:50 PM

loads ok, but doesn't seem to want to change. Or maybe I am calling it wrong, with this from the select box:

<select id="locale" name="locale" onchange="if( this.value ){ window[ lang=this.value ] }" style="float:left">

Old Pedant
09-06-2011, 08:04 PM
Oh, you can't do that.

You have to use document.write *BEFORE* the page is fully loaded!

So by definition you can't use it when the user changes some <select>, as that is long long after the page is loaded.

Personally, I'd just do

<select onchange="if( this.value ){ location.href='urlOfThisPage.html?lang=' + this.value; }">

and then, when the page loads, look in the query string for lang="xxx" and use the document.write I showed.

09-06-2011, 08:08 PM

so, coming back to my original question, the answer is no?

Old Pedant
09-06-2011, 09:23 PM
Well, yes and no.

You *can* cause a JS script to be loaded after the page is loaded, and then you can activate it.

But in the case of something like the Google Maps API, you wouldn't be able to display a map and *then* change the language. Their API isn't set up for that, I don't believe. (But check the latest docs...might have changed since I worked with it.)

So it's probably a big fat "it depends" on the API in question.

Old Pedant
09-06-2011, 09:24 PM
As a sneaky trick: With the Google Maps API, you *could* put the map into an <iframe> and then simply re-load only the <iframe> if the user changes the language.

But it seems to me that if you are going to change the language of the map, you'd want to also change the language of most of the page, so is this really a good thing to do??? You would know your own situation best, of course.

09-07-2011, 03:00 AM
it is sneaky, and I love it.

thanks again, Old Pedant!