2

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?

3
  • 2
    what happens? Does it give you an error? Is it just blank? Did you try removing the window.location.host? You don't really need it. Commented Jun 20, 2012 at 21:39
  • 1
    Shouldn't all of that code be in a document.ready function? Commented Jun 20, 2012 at 21:40
  • @Connor: Not if it's after the elements in question. Commented Jun 20, 2012 at 21:48

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use window.location.origin, but only on WebKit-based browsers.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.