简体   繁体   中英

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. Namely, the source content is a fixed height div that allows scrolling, but without the scrollbar. If I load that up in an iframe, it won't have the same effect. 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...

On the source server, I tried writing all of the content to a php variable like this:

<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:

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

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. Such things will pop up as syntax errors in Firefox's Error Console, an invaluable tool when developing 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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