私はthis websiteに反応しています。これはJavaScriptを使用してすべてのページにindex.html
をロードする静的なHTML Webサイトです。JavaScriptはURLに#を入れ、新しいページが途中で開きます
問題はJavaScriptコードがすべてのリンクの前に#
を配置することです。ページの途中でリンクをクリックすると、途中で新しいページが開きます(古いページ上のリンクと同じスクロール位置)。
新しいページを常にページの上部で開くことができますか?
JavaScriptファイルは、(私にはちんぷんかんぷんです...)以下のスクリプト
var ScriptJS = true;
var lastLocation = document.location.href;
$(document).ready(function() {
if (!$('#ajax').length) { // Check if index.html was loaded. If not, navigate to index.html and load the hash part with ajax.
document.location = document.location.href.substring(0,
document.location.href.lastIndexOf('/') + 1) +
"#" +
document.location.href.substr(document.location.href.lastIndexOf('/') + 1);
}
if (window.location.hash) // Check if the page is being loaded with a '#'. Load the file.
$('#ajax').load(window.location.hash.substr(1));
});
$(document).on("click", "a:not(.regular)", function (e) {
var url = this.href;
if (url.indexOf("https") != -1 || url.indexOf('.html') == -1) // External link or picture
return;
e.preventDefault();
$('#ajax').load(url, function() {
var pagename = url.substr(url.lastIndexOf('/') + 1);
lastLocation = document.location.href.replace(window.location.hash, "")
+ '#' + pagename;
document.location.href = '#' + pagename;
});
});
window.onpopstate = function (event) {
if (lastLocation != document.location.href)
location.reload();
else
return;
};
'document.location.href = '#' + pagename;はあなたの犯人のようです。 – sesamechicken