简体   繁体   English

如何包含来自外部站点的内容?

[英]how to include content from an external site?

This is a great big mess... 这是一个很大的混乱...

I know I could use an iframe, but the problem there is that there are some cross-site limitations that wind up botching what I'm trying to do. 我知道我可以使用iframe,但问题是存在一些跨站点限制,最终限制了我要执行的操作。 Namely, the source content is a fixed height div that allows scrolling, but without the scrollbar. 即,源内容是允许滚动但没有滚动条的固定高度div。 If I load that up in an iframe, it won't have the same effect. 如果我将其加载到iframe中,则效果不一样。 It either insists on having a scrollbar, or it doesn't scroll at all. 它要么坚持要有滚动条,要么根本不滚动。 I've been trying to find a way around this, but the destination server doesn't have php available , so I'm flat-out stuck on how to do this... 我一直在尝试解决此问题的方法,但是目标服务器没有可用的php ,因此我对如何执行此操作一无所知。

On the source server, I tried writing all of the content to a php variable like this: 在源服务器上,我尝试将所有内容写入这样的php变量中:

<script type="text/javascript">
var mycontent = '<?php print $content; ?>';
</script>

and then calling that variable in my test.js file and when I use an alert, I get the text (also on the source server), like this: 然后在我的test.js文件中调用该变量,当我使用警报时,我得到了文本(也在源服务器上),如下所示:

var content = mycontent;
alert(content);

It works, but, not from the destination server. 它可以工作,但是不能从目标服务器上工作。 So, on the destination server, if I use: 因此,在目标服务器上,如果我使用:

 <script src="http://mysite.com/test.js"></script>

And try to use: 并尝试使用:

<script type="text/javascript">document.write('content');</script>

It doesn't print anything out... I'm assuming because of cross-site limitations? 它没有打印出任何内容...我想是因为跨站点限制? I'm desperate for a workaround... Can anyone help out? 我迫切需要一个解决方法...有人可以帮忙吗?

It should be perfectly well possible to do a 做一个

<script type="text/javascript" src="http://mysite.com/test.js"></script>

and in the Javascript, without <script type.... , do 并且在Javascript中, 没有 <script type....

document.write('blah');
document.write('blah');
document.write('blah');
document.write('blah');
document.write('blah');

this will work fine across domains. 这将在所有域中正常工作。 I think your problem is that you embed <script> tags into the js file, which is wrong. 我认为您的问题是您将<script>标记嵌入到js文件中,这是错误的。 Such things will pop up as syntax errors in Firefox's Error Console, an invaluable tool when developing JavaScript. 这样的事情会在Firefox的错误控制台中以语法错误的形式弹出,这是开发JavaScript时不可估量的工具。

There's a downside to this approach by the way: If your target server serving the JS is down, the rendering of the document that embeds the script will be delayed. 顺便说一句,这种方法有一个缺点:如果为JS提供服务的目标服务器已关闭,则嵌入脚本的文档的呈现将被延迟。

发出服务器端 XHR请求,获取内容,并在页面加载完毕后将其包括在内。

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

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