jquery load调用导致空查询字符串

时间:2013-04-19 01:54:19

标签: javascript jquery query-string jquery-load

我有这个文件:“myFile.html”我只想在查询字符串中显示xxx的值。我在其他线程的帮助下提取了以下内容。我使用了purl和自定义功能。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="js/purl.js"></script>

<script type="text/javascript">
$(document).ready(function(){
    console.debug($.url().param('xxx'));
    console.debug(getParameterByName('xxx'));
}); 

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>

从firefox调用url:“localhost .... / myFile.html?xxx = hi”工作正常,xxx的值在purl和自定义函数的控制台中显示。

但是,如果我在另一个页面“myHome.html”中通过JQuery.load加载“myFile.html”,则xxx的值会产生一个空字符串:

<script type="text/javascript">
$(document).ready(function(){
    $('#myDiv').load("myFile.html?xxx=hi");
}); 
</script>

我无法弄明白为什么......

2 个答案:

答案 0 :(得分:0)

我认为您所看到的行为是预期的,因为您正在使用行

$('#myDiv').load("myFile.html?xxx=hi");

将原始HTML转储到myDiv div标签中。包含页面的URL不受此操作的影响。因此,对xxx查询字符串参数的调用不会返回任何内容,因为该参数嵌入在div标记中,而不是附加到查询字符串。

答案 1 :(得分:0)

我在这里找到答案: jquery get querystring of loaded page in a div

似乎不可能,但使用回调函数我克服了这个问题。

相关问题