Patrick Murray
Patrick Murray

Reputation: 108

Issue while using the jQuery function .load()

I am having an issue on my site where I am attempting to load the contents of a PHP file into a <div>. I have ruled out the possibility that it is a server-side issue, so this leads me to my question. Can you find anything wrong with the following code?

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

I am aware that for security reasons, browsers do not allow .load() to load content from external domains; however, would using window.location.host be an issue as it is the same domain?

Upvotes: 2

Views: 74

Answers (2)

Ry-
Ry-

Reputation: 224962

window.location.host only includes the host name, not the protocol, which is necessary. Include that too:

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

Of course, you probably don't even need that; a leading / will get you an absolute URL:

$(".content").load("/index.php?url=" + toLoad);

Upvotes: 2

idrumgood
idrumgood

Reputation: 4924

Try using window.location.hostname as window.location.host also includes port number and sometimes other characters

Upvotes: 1

Related Questions