Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts

    change parameters of loaded script after page load?

    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:
    Code:
    <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:
    Code:
    <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:
    Code:
    <script type="text/javascript">
    	var lang="es";
    	</script>
    <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?

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    yeah, I know I can do that, but what I'm asking is if I can do it *without* reloading the page...

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,933
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    In other words:
    Code:
    <script type="text/javascript">
    var lang="es";
    document.write(
          '<sc' + 'ript type="text/javascript" '
        + ' src="http://maps.google.com/maps/api/js?sensor=false&language=' + lang + '">'
        + '</sc' + 'ript>');
    </script>
    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.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    hmmm...

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

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

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,933
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    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
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    right.

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

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,933
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,933
    Thanks
    79
    Thanked 4,424 Times in 4,389 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    xelawho (09-07-2011)

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    it is sneaky, and I love it.

    thanks again, Old Pedant!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •