私は、いくつかのphpコードを使用して動的にロードされるWebサイトを作成して、他のさまざまなファイルからコンテンツをスワップアウトすることに取り組んでいます。その上で、jquery を使用してこのスワップ アウトを美しくしています。
関連する PHP:
<?php
// Set the default name
$page = 'home';
// Specify some disallowed paths
$disallowed_paths = array('js.php','css.php','index.php');
if (!empty($_GET['page'])){
$tmp_page = basename($_GET['page']);
// If it's not a disallowed path, and if the file exists, update $page
if (!in_array($tmp_page, $disallowed_paths) && file_exists("{$tmp_page}.php"))
$page = $tmp_page;
}
// Include $page
if(!file_exists("$page.php")){
$page = 'home';
}
else{
include("$page.php");}
?>
関連するjquery:
$(function (a) {
var $body = $('body'),
$dynamo = $('.dynamo'),
$guts = $('.guts');
$body.fadeIn('slow');
if (history.pushState) {
var everPushed = false;
$dynamo.on('click', 'a', function (b) {
var toLoad = $(this).attr('href');
history.pushState(null,'',toLoad);
everPushed = true;
loadContent(toLoad);
b.preventDefault();
});
$(window).bind('popstate', function () {
if (everPushed) {
$.getScript(location.href);
}
everPushed = true;
});
} // otherwise, history is not supported, so nothing fancy here.
function loadContent(href) {
$guts.hide('slow', function () {
$guts.load(href, function () {
$guts.show('slow');
});
});
}
a.preventDefault();
});
?page=about を使用してページにリンクすると何が起こっていますか?page=about は機能しますが、コンテンツを交換するだけでなく、動的に読み込まれた div 内のコンテンツを含むページ全体を読み込みます。
私が about.php に直接リンクした場合、それは何の問題もなく美しく機能しますが、そのリンクhttp://example.com/about.phpを共有したい人は誰でも、CSS スタイリングなしでコンテンツだけを取得します。
「loadContent」の jquery を次のように変更するとします。
$guts.load(href + ' .guts', function () {
編集1:
, ' .guts'
に
+ ' .guts'
その後は機能しますが、コンテンツのロード時に削除されるため、スクリプト、Java、jquery などは機能しません。
http://kazenracing.com/st3をチェックしてください
EDIT 2 : また、サイトが実際には正しく機能していないようです。背景もアイコンも表示されませんが、それでも一般的な問題は発生します。
私はそれを自分で修正できます。それには少し時間がかかります。今のところ、私の焦点は再帰問題にあります。
EDIT 3 : サイトに取り組む必要はなくなりました。問題は解決されませんでしたが、そのプロジェクトを手放します。そのため、リンクは有効な URL ではなくなりました。