Luican Adrian
Luican Adrian

Reputation: 99

jQuery Load Page Error

I'm trying to load a page when a div from my content is loaded , but isn't working. Can you help me and tell me where is my error or why isn't working ? Thanks !

Here's the code

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/fonts.css" rel="stylesheet" type="text/css" />
</head>

<body>
<ul id="nav">
<li><a href="index">index</a></li>
<li><a href="about">contact</a></li>
</ul>

<div id="content"></div>
<script src="http://code.jquery.com/jquery-2.0.2.min.js" type="text/javascript"></script>
<script src="js/js.js" type="text/javascript"></script>
</body>
</html>

hello.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<h1>Hello World</h1>
</body>
</html>

And javascript

$(document).ready(function() {
$('#content').load('hello.php');
});

Upvotes: 0

Views: 127

Answers (3)

isJustMe
isJustMe

Reputation: 5470

Change your html like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="css/fonts.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-2.0.2.min.js" type="text/javascript"></script>
<script src="js/js.js" type="text/javascript"></script>

</head>

<body>
<ul id="nav">
<li><a href="index">index</a></li>
<li><a href="about">contact</a></li>
</ul>

<div id="content"></div>

</body>
</html>

And your Jquery like th is:

$("#content").load("/hello.php", function(response, status, xhr) {
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $("#error").html(msg + xhr.status + " " + xhr.statusText);
  }
});

This should work, and if not it will let you know what happened.

Upvotes: 0

Khanh TO
Khanh TO

Reputation: 48972

You have to make sure this script tag

<script src="http://code.jquery.com/jquery-2.0.2.min.js" type="text/javascript"></script>

is executed before

$(document).ready(function() {
     $('#content').load('hello.php');
});

Upvotes: 0

Amar Banerjee
Amar Banerjee

Reputation: 5012

Use this in head section

<script src="http://code.jquery.com/jquery-2.0.2.min.js" type="text/javascript"></script>
<script src="js/js.js" type="text/javascript"></script>

Upvotes: 1

Related Questions