模拟PHP包含没有PHP [英] Simulate PHP Include Without PHP

查看:132
本文介绍了模拟PHP包含没有PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多个页面中包含相同的导航菜单,但是我没有PHP支持,也不能以任何其他方式影响我的服务器。



I希望避免将html复制并粘贴到所有页面上,因为这会使菜单更新变得很痛苦。



我能想到的两个选项如下所示:



1)将所有内容存在于一个页面上,然后根据附加到url的关键字确定显示哪些内容:

  example.com/index?home 
example.com/index?news

2)包含一个JavaScript文件,该文件具有将菜单写出并在每个页面上调用该函数的功能

  function setupMenu(){
$(#nav)。html(< ul class ='nav'>< li> home< / li>< li>新闻和LT; /立GT;< / UL>中);





$ b

使用选项1,更新过程将包括编辑一个导航菜单one page



使用选项2,更新将意味着更改javascript文件中的函数



1是页面将不得不加载很多不需要显示的内容。我担心选项2可能看起来微不足道,但它是代码可能会变得混乱。



有没有什么理由以一种方式比另一种更好?或者是否有第三个优先选择,我缺少?

解决方案

您有几个选项,每个选项都有自己的优点和缺点:$ b​​
$ b


  • 服务器端包含或SSI。如果你没有PHP,那么你很有可能没有SSI,而这个选项需要对你的.htaccess文件进行一些令人讨厌的分析。检查Dominic P.的SSI写作答案。 SSI优于JavaScript或Frames的好处是它不需要用户启用JS(很多用户不需要),也不会导致任何导航困难。


  • 框架。您可以使用标准框架将导航置于其自己的单独文件中,并使用正确的造型它将无缝。您也可以使用 iframe 将导航放置在网站的任意部分,例如边栏或其他内容。帧,特别是标准帧的缺点是它们倾向于制作书签,链接和前进/后退按钮的行为奇怪。好处是,框架不需要浏览器合规性或服务器支持。

  • JavaScript。您可以参考任何其他解答,以获得JS解决方案的出色解释,特别是在您使用jQuery时。但是,如果您的网站没有足够的动态性,以至于您的用户希望启用JavaScript,则这意味着大量的观看者根本看不到菜单 - 确实很糟糕。


    -

I want to include the same navigation menu on multiple pages, however I do not have PHP support, nor can I affect my server in any other way.

I want to avoid simply copying and pasting the html onto all the pages as this would make updating the menu a pain.

The two options I can think of are as follows:

1) Have all the content exist on one page, then determine which content to show based on a keyword appended to the url:

example.com/index?home
example.com/index?news

2) Include a javascript file that has a function that writes the menu out and call the function on each page

function setupMenu() {
    $("#nav").html("<ul class='nav'><li>home</li><li>news</li></ul>");
}

With Option 1, the updating process would consist of editing one nav menu on the one page

With Option 2, updating would mean changing the function in the javascript file

My concern with Option 1 is that the page would have to load a lot of content that it wouldn't need to display. My concern for Option 2 may seem trivial but it is that the code can get messy.

Are there any reasons doing it one way would be better than the other? Or is there a third superior option that I'm missing?

解决方案

You have a few options, each with its own advantages and drawbacks:

  • Server Side Includes, or SSI. If you don't have PHP there's a good chance you don't have SSI either, and this option requires some irritating mucking-about with your .htaccess file. Check Dominic P.'s answer for a writeup of SSI. The benefit of SSI over JavaScript or Frames is that it doesn't require the user to have JS enabled - which a lot of users don't - and it also doesn't present any navigational difficulties.

  • Frames. You could either use standard frames to put the navigation in its own separate file, and with the correct styling it would be seamless. You could also use an iframe to place your navigation in an arbitrary part of the site, like a sidebar or whatever. The downside to frames, particularly standard frames, is that they tend to make bookmarking, links and the forward/back buttons behave oddly. On the upside, frames don't need browser compliance or server support.

  • JavaScript. You can refer to any of the other answers for excellent explanations of the JS solution, particularly if you're using jQuery. However, if your site isn't otherwise dynamic enough that your users will want to have JavaScript enabled, this will mean that a large number of your viewers will not see the menu at all - bad, definitely.

  • -

这篇关于模拟PHP包含没有PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆