【问题标题】:.load() for loading htm in htm with parameter.load() 用于在带有参数的 html 中加载 htm
【发布时间】:2016-04-10 23:03:02
【问题描述】:

在 Index.html 我有:

  <head> 

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

  </head>

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

除了参数 ?city=London 外,一切正常

我的意思是 page.htm 在 index.htm 中打开,但参数 city 不可见。 它应该有效吗?

在 page.htm 我有

<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>

最后

<div class="city"></div>
<script>
  var cityVar= getUrlParameter('city');
  $('.city').html(cityVar);
</script>

【问题讨论】:

标签: javascript


【解决方案1】:

尝试将查询字符串作为第二个参数传递:

$(".myDIV").load("page.html", { city: 'London' });

【讨论】:

  • 它没有帮助。 link 这里写着: URL - 必填。指定要加载的 URL。所以如果我需要的 url 是 page.htm?city=London 我认为它的语法很好?
【解决方案2】:

我认为您的问题是您尝试使用 javascript 而不是在服务器端检索查询字符串。

虽然服务器会看到您在 load 中传递的查询字符串,但 javascript 会看到当前的 url。

例如,您可以简单地执行以下操作:

<script>
   var city = "<?php echo $_GET['city']; ?>";
</script>

【讨论】:

  • in page.html &lt;script&gt; var cityVar = getUrlParameter('city'); alert(cityVar); &lt;/script&gt; 我在 page.html 中执行此操作,它已在 index.html 中加载,但参数 city 未定义。
  • 是的,这是因为您尝试通过 javascript 检索它,它访问的是 页面 的当前 url,而不是您通过 load 函数传递的 url。
  • 你对"&lt;?php echo $_GET['city']; ?&gt;";的回答应该在page.htm中?
  • 是的,这是您有兴趣从查询字符串中提取city 变量的页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多