简体   繁体   English

通过js include将html文件包含在html中

[英]Include php file with html via js include

Ok so I am tasked to work with this third party vendor that shows movie times on their end. 好的,我的任务是与这个第三方供应商合作,在他们的最后显示电影时间。 We have the chance to co-brand our site on their end, so in order to do that we need to create a wrapper of our site so that the movie times can show inside the wrapper so it still looks like your on our site. 我们有机会在网站的末端联合品牌,因此,为了做到这一点,我们需要创建网站的包装,以便电影时间可以在包装中显示,因此看起来仍然像您在我们网站上的样子。 One option is to have a header and footer included via js external file, that way we can make changes on our side and the updates will show on their side, so we dont have to send them a new header/footer every time we make updates to the wrapper. 一种选择是通过js外部文件包含页眉和页脚,这样我们可以在一侧进行更改,并且更新将显示在它们的一侧,因此我们不必在每次进行更新时都发送新的页眉/页脚到包装纸。 They suggested we do it this way: 他们建议我们这样做:

<script src="http://oursite.com/header.php"></script>
<script src="http://oursite.com/footer.php"></script>

So I have raw html in header.php hosted on our side and then I added this in the header before anything else: 因此,我在header.php中托管了原始html,然后在其他任何内容中将其添加到了header中:

<?php header("Content-type: text/javascript"); ?>

However when the page loads I get this error: 但是,当页面加载时,出现此错误:

SyntaxError: syntax error

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xml

Im assuming there is a problem having raw html code in a js file? 我假设在js文件中包含原始html代码有问题吗? If so is there anyway around this? 如果可以的话,这附近有没有?

You should add a link to proper javascript that push desired html to the page. 您应该添加指向正确的javascript的链接,以将所需的html推送到页面。

<script src="http://oursite.com/[somejsfile].js"></script>

You can get help from this answer on how to insert html elements using javascript 您可以从此答案中获得有关如何使用javascript插入html元素的帮助

If you are going to use javascript to call an external file you need to use ajax. 如果要使用javascript调用外部文件,则需要使用ajax。 The problem then lies that you are taking about your site and that cannot be done when your domains do not match. 那么问题就出在您要管理的站点上,而当您的域不匹配时则无法完成。

If you do it in the manner you have included you must declare the html in javascript variables then, on load push the data to where it needs to go. 如果以包含的方式进行操作,则必须在javascript变量中声明html,然后在加载时将数据推送到需要的位置。

 var html = '<html> YOUR CODE WATCH OUT FOR SINGLE QUOTES </html>';
 //check for load with jquery
 $(document).ready(function(){
   $('#header").html(html);
 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM