1

我正在制作一个动态加载的网站,使用一些 php 代码来交换各种其他文件的内容。最重要的是,我使用 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();
    });
  1. 发生的情况是,当我使用 ?page=about 链接到页面时,它会起作用,但它不会只是换出内容,而是会加载整个页面,包括动态加载的 div 中的内容。

  2. 如果我要直接链接到 about.php,它会很好地工作,没有任何错误,但是任何想要共享该链接http://example.com/about.php的人都会得到没有 css 样式的内容。

  3. 如果我要将“loadContent”中的 jquery 更改为:

    $guts.load(href + ' .guts', function () {
    

编辑 1

    , ' .guts'

    + ' .guts'

然后它会工作,但任何脚本、java、jquery 或类似的东西都不会工作,因为它会在内容加载时被剥离。

查看http://kazenracing.com/st3

编辑 2:该网站似乎也无法在野外正常工作。没有背景,也没有显示任何图标,但它仍然解决了一般问题。

我可以自己解决,只是需要一些时间。现在我的重点是递归问题。

编辑 3:网站不再需要工作,问题从未解决,但我们正在放手该项目。因此,该链接不再是有效的 URL。

4

0 回答 0