0

In Index.html I have:

  <head> 

  <script> 
    $(function(){ $(".myDIV").load("page.htm?city=London"); });
  </script>

  </head>

 <body>
     <div class="myDIV"></div>
 </body>

everything works except parameter ?city=London

I mean page.htm is opening inside index.htm but parameter city is not visible. Should it works?

In page.htm I have

<script>

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

</script>

and finally

<div class="city"></div>
<script>
  var cityVar= getUrlParameter('city');
  $('.city').html(cityVar);
</script>
keid
  • 91
  • 1
  • 10
  • `load` function takes the query string of your current url. Try access `index.html?city=somethingElse` and see what happens. – guyaloni Jan 06 '16 at 18:57
  • http://stackoverflow.com/questions/31368864/how-to-access-url-parameters-sent-to-div-via-jquery-load This works for me. – keid Jan 06 '16 at 20:13

2 Answers2

0

Try by passing querystring as second argument:

$(".myDIV").load("page.html", { city: 'London' });
  • It doesnt help. [link](http://www.w3schools.com/jquery/ajax_load.asp) Here it is written: URL - Required. Specifies the URL you wish to load. So if my required url is page.htm?city=London I think its good syntax? – keid Jan 06 '16 at 19:23
0

I think that your problem is that you try to retrieve the query string using javascript and not in server side.

While server will see the query string that you pass inside load, javascript will see the current url.

For example, you can simply do something like:

<script>
   var city = "<?php echo $_GET['city']; ?>";
</script>
guyaloni
  • 4,972
  • 5
  • 52
  • 92
  • in page.html `` I did this in page.html and it's loaded in index.html but parameter city is undefined. – keid Jan 06 '16 at 19:07
  • Yes, this is because you try to retrieve it via javascript, which access the current url ot *the page*, not the url which you pass by `load` function. – guyaloni Jan 06 '16 at 21:00
  • your answer with `"";` It should be in page.htm? – keid Jan 06 '16 at 22:59
  • Yes, this is the page which you are interested in extracting `city` variable from the query string. – guyaloni Jan 07 '16 at 10:08