У меня есть небольшая проблема с моим скриптом здесь. По какой-то причине он не позволяет # -tags, и я не знаю почему. Я создал этот javascript с помощью этого tutorial. (Загрузка страниц хорошо работает без каких-либо проблем.)Ajax URL # не обновляется
Может кто-нибудь, пожалуйста, просмотрите его и скажите, почему это не работает?
var default_content="";
$(document).ready(function(){ //executed after the page has loaded
checkURL(); //check if the URL has a reference to a page and load it
$('ul li a').click(function (e){ //traverse through all our navigation links..
checkURL(this.hash); //.. and assign them a new onclick event, using their own hash as a parameter (#page1 for example)
});
setInterval("checkURL()",250); //check for a change in the URL every 250 ms to detect if the history buttons have been used
});
var lasturl=""; //here we store the current URL hash
function checkURL(hash)
{
if(!hash) hash=window.location.hash; //if no parameter is provided, use the hash value from the current address
if(hash != lasturl) // if the hash value has changed
{
lasturl=hash; //update the current hash
loadPage(hash); // and load the new page
}
}
function loadPage(url) //the function that loads pages via AJAX
{
// Instead of stripping off #page, only
// strip off the # to use the rest of the URL
url=url.replace('#','');
$('#loading').css('visibility','visible'); //show the rotating gif animation
$.ajax({
type: "POST",
url: "load_page.php",
data: 'page='+url,
dataType: "html",
success: function(msg){
if(parseInt(msg)!=0) //if no errors
{
$('#content').html(msg); //load the returned html into pageContet
} $('#loading').css('visibility','hidden');//and hide the rotating gif
}
});
}
Что на самом деле происходит с этим? Вы должны дать более подробную информацию о том, что вы делаете, чтобы создать ошибку, что на самом деле происходит неправильно и что вы ожидаете. На данный момент совершенно неясно, не будет ли его загрузка страниц, независимо от того, не делает ли она ajax или что-то еще. Получаете ли вы какие-либо сообщения об ошибках в консоли javascript? – Chris
Вы действительно должны использовать событие hashchange, оно хорошо поддерживается с IE8: https://developer.mozilla.org/en-US/docs/Web/API/window.onhashchange – nullability
@nullability Спасибо за помощь и извините спросите, можете ли вы показать m2, как мне это включить? когда я это пробовал, моя страница не загружалась ... что-то вроде hashChange не определено – adho12