Web Analytics Made Easy -
StatCounter change parameters of loaded script after page load? - CodingForum

Announcement

Collapse
No announcement yet.

change parameters of loaded script after page load?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.

    Comment


    • #3
      yeah, I know I can do that, but what I'm asking is if I can do it *without* reloading the page...

      Comment


      • #4
        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.)
        Be yourself. No one else is as qualified.

        Comment


        • #5
          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">

          Comment


          • #6
            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.
            Be yourself. No one else is as qualified.

            Comment


            • #7
              right.

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

              Comment


              • #8
                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.
                Be yourself. No one else is as qualified.

                Comment


                • #9
                  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.
                  Be yourself. No one else is as qualified.

                  Comment


                  • #10
                    it is sneaky, and I love it.

                    thanks again, Old Pedant!

                    Comment

                    Working...
                    X
                    😀
                    🥰
                    🤢
                    😎
                    😡
                    👍
                    👎