2

我在尝试将 PHP 文件的内容加载到<div>. 我已经排除了这是服务器端问题的可能性,所以这引出了我的问题。你能发现下面的代码有什么问题吗?

<script>
$('.navigation .responsive .menu ul li a').click(function()
{
    var toLoad = $(this).attr('href');
    $(".content").load(window.location.host + "/index.php?url=" + toLoad);
});
</script>

我知道出于安全原因,浏览器不允许.load()从外部域加载内容;但是,使用window.location.host会是一个问题,因为它是同一个域吗?

4

2 回答 2

2

window.location.host只包括主机名,不包括必要的协议。也包括:

$(".content").load(window.location.protocol + '//' + window.location.host + "/index.php?url=" + toLoad);

当然,您可能甚至不需要它;前导/将为您提供绝对 URL:

$(".content").load("/index.php?url=" + toLoad);
于 2012-06-20T21:40:55.997 回答
1

尝试使用window.location.hostnameaswindow.location.host还包括端口号,有时还包括其他字符

于 2012-06-20T21:39:54.983 回答