通过javascript在html中包含html [英] Include html in html through javascript

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

问题描述

我的问题与此相似
如何在没有frame / iframe的情况下将HTML页面包含到另一个HTML页面中?

但是答案对我来说并不合适。对象和iframe创建一个带滚动条的框,我不想要它,如果我有其他页面的链接,我希望整个页面都可以更改,而不仅仅是框内的内容。



此指令不起作用

<! - #include virtual =/ footer.html - >



我只想使用html,因为这是网站建立的方式,我们不会重新设计它。我只是这样做的内容修改目的。

在javascript文件中编写html页眉(或页脚)是一种不好的做法,然后在所有文件中包含.js我的html文件?






第1次编辑

我有大约70个不同的html静态页面。
我想更新内容,如标志,菜单,网站的元描述,文本颜色等,但目前,我必须对这些修改进行70次不同的修改。 p>

我使用javascript作为菜单的html部分,因为菜单与JavaScript无论如何处理过,并且包含了所有我的html文件中的文件。

 函数writeJS(){
var strVar =;
strVar + =< body bgcolor = \#000000\>;
strVar + =< div class = \Main_container \>;
...
document.write(strVar);
}
writeJS();

我不知道对这些标签做同样的做法是好还是不好,

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp: //www.w3.org 
/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< meta name =description...






第二次编辑



虽然html / javascript选项可行,但我们决定投入几天时间去寻找wordpress.org网站(从长远来看更便宜,更好)。

解决方案


完全不工作




 <! - #include virtual =/ footer.html  - > 

...可能因为SSI未启用。据推测,你告诉我们这是因为你认为它会给你你想要的结果 - 暗示问题的一部分是为什么它没有工作?你做了什么调查?


在JavaScript文件中编写html页眉(或页脚)是一种不好的做法, .js在所有的html文件中?

这不是坏习惯 - 它不会起作用 - 你可以从javascript注入html - 但你需要JavaScript代码来做注射。这也与您之前的声明相矛盾:

lockquote
我只想使用html
$ /
$ b

您提供的代码不会插入html,它会附加它。 document.write的问题在于它只写入到文档输入流的末尾。如果该流已关闭,则会重新打开并清除当前的HTML。从来没有好的理由使用document.write()。考虑:

 < html> 
< div id =header>
< noscript> ....< / noscript>
< / div>
< div id =page_specific_content>
...
< / div>
< div id =footer>
< noscript> ....< / noscript>
< / div>
< / html>

和...

  function addHeader()
{
var el = document.getElementById(header);
if(el){
el.innerHTML =...;
}
}

但是,这仍然是写一个动态网站。即使您不想在生产服务器上运行ASP或PHP或PERL或服务器JS或....在使用适当的CMS或模板系统进行开发时,也会更有意义,然后再将HTML获取发布的静态版本。


My question is similar to this one How to include an HTML page into another HTML page without frame/iframe?

But the answers are not working for me. Objects and iframes create a box with scrollbars, which I don't want, and if I have links to other pages, I would like the whole page to change, not just what's inside the box.

This instruction doesn't work

<!--#include virtual="/footer.html" -->

I want to use only html, because this is how the website has been built and we are not redesigning it. I am only doing this for content modification purposes.

Is it a bad practice to write an html header (or footer) in a javascript file then include the .js in all of my html files?


1st edit

I have about 70 different html static pages. I want to update contents such as the logo, the menu, the meta description of the web site, the text color, etc. but at the moment, I have to make each of these modifications 70 different times.

I used javascript for the html part of the menu, because the menu worked with javascript anyway, and included the file in all of my html files.

function writeJS(){
    var strVar="";
    strVar += "<body bgcolor=\"#000000\">";
    strVar += "<div class=\"Main_container\">";
    ...
    document.write(strVar);
}
writeJS();

I don't know if it is a good or bad idea to do the same with those tags for example.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" ...


2nd edit

Although the html/javascript option was viable, we decided to invest a couple of days to go for a wordpress.org site (cheaper and better in the long run).

解决方案

This instruction doesn't work at all

<!--#include virtual="/footer.html" -->

...probably becaue SSI is not enabled. Presumably you're telling us this because you think it would give you the result you want - implying that part of the question is why didn't it work? What did you do to investigate?

Is it a bad practice to write an html header (or footer) in a javascript file then include the .js in all of my html files?

It's not about bad practice - it won't work - you can inject html from javascript - but you need javascript code to do the injecting. Also this contradicts your earlier statement:

I want to use only html

The code you've provided does not inject html it appends it. The problem with document.write is that it only writes to the end of the document input stream. And if that stream is already closed, it re-opens it and clears the current HTML. There's never a good reason to use document.write(). Consider:

<html>
   <div id="header">
     <noscript>....</noscript>
   </div>
   <div id="page_specific_content">
   ...
   </div>
   <div id="footer">
     <noscript>....</noscript>
   </div>
 </html>

and...

 function addHeader()
 {
   var el=document.getElementById("header");
   if (el) {
      el.innerHTML="...";
   }
 }

But this is still a bad way to write a dynamic website. Even if you don't want to run ASP or PHP or PERL or serverside JS or.... on your production server, it'd make a lot more sense to to dfevelop using a proper CMS or templating system then scrape the HTML to get a static version for publication.

这篇关于通过javascript在html中包含html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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