0
Me and a friend have been having some problems with jQuery..
If we base it on a code to make the button back and forth of the browser work on our site, with the function of a update div without refresh...
The code is working in parts, but I’m having to deal with the following errors:
1º When I enter page 1, then page 2 and try to go back through the browser arrow, the content, site title and url.
Follows the code:
<?php
$isXHR = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) === 'XMLHTTPREQUEST';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Inicio</title>
<script src="jquery.min.js"></script>
<script>
$(function () {
var load = function (url) {
$.get(url).done(function (data) {
var title = $(data).filter('title').text();
$("title").html(title);
$("#content").html(data);
})
};
$(document).on('click', 'a', function (e) {
e.preventDefault();
var $this = $(this),
url = $this.attr("href"),
title = title;
history.pushState({
url: url,
title: title
}, title, url);
document.title = title;
load(url);
});
$(window).on('popstate', function (e) {
var state = e.originalEvent.state;
if (state !== null) {
document.title = state.title;
load(state.url);
} else {
//var title = $(state).filter('title').text();
//document.title = title;
$("#content").empty();
}
});
});
</script>
</head>
<body>
<?php
if (!$isXHR) {
echo "
<ul>
<li><a href=\"index.php\">Inicio</a></li>
<li><a href=\"abc.php\">Página 2</a></li>
<li><a href=\"abc2.php\">Página 3</a></li>
</ul>
";}
?>
<div id="content">Página 1</div>
</body>
</html>
abc.php (page 2)
<?php
$isXHR = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) === 'XMLHTTPREQUEST';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Página 2</title>
<script src="jquery.min.js"></script>
<script>
$(function () {
var load = function (url) {
$.get(url).done(function (data) {
var title = $(data).filter('title').text();
$("title").html(title);
$("#content").html(data);
})
};
$(document).on('click', 'a', function (e) {
e.preventDefault();
var $this = $(this),
url = $this.attr("href"),
title = title;
history.pushState({
url: url,
title: title
}, title, url);
document.title = title;
load(url);
});
$(window).on('popstate', function (e) {
var state = e.originalEvent.state;
if (state !== null) {
document.title = state.title;
load(state.url);
} else {
//var title = $(state).filter('title').text();
//document.title = title;
$("#content").empty();
}
});
});
</script>
</head>
<body>
<?php
if (!$isXHR) {
echo "
<ul>
<li><a href=\"index.php\">Inicio</a></li>
<li><a href=\"abc.php\">Página 2</a></li>
<li><a href=\"abc2.php\">Página 3</a></li>
</ul>
";}
?>
<div id="content">Página 2</div>
</body>
abc2.php (page 3)
<?php
$isXHR = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) === 'XMLHTTPREQUEST';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Página 3</title>
<script src="jquery.min.js"></script>
<script>
$(function () {
var load = function (url) {
$.get(url).done(function (data) {
var title = $(data).filter('title').text();
$("title").html(title);
$("#content").html(data);
})
};
$(document).on('click', 'a', function (e) {
e.preventDefault();
var $this = $(this),
url = $this.attr("href"),
title = title;
history.pushState({
url: url,
title: title
}, title, url);
document.title = title;
load(url);
});
$(window).on('popstate', function (e) {
var state = e.originalEvent.state;
if (state !== null) {
document.title = state.title;
load(state.url);
} else {
//var title = $(state).filter('title').text();
//document.title = title;
$("#content").empty();
}
});
});
</script>
</head>
<body>
<?php
if (!$isXHR) {
echo "
<ul>
<li><a href=\"index.php\">Inicio</a></li>
<li><a href=\"abc.php\">Página 2</a></li>
<li><a href=\"abc2.php\">Página 3</a></li>
</ul>
";}
?>
<div id="content">Página 3</div>
</body>
</html>
About possible duplicate... this way that you inform, can not return the content with the arrow of the browser. Nor did they give a specific answer.
Possible duplicate -> https://answall.com/questions/276897/problemas-com-jquery
– Valdeir Psr
Possible duplicate of Problems with Jquery
– Sam