iframe v / s外部js(在其他网站上显示我的网站内容) [英] Iframe v/s external js (to show my website's content on other websites)

查看:142
本文介绍了iframe v / s外部js(在其他网站上显示我的网站内容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,

我创建了一个代码片段,可以复制然后放入任何网站。它在其他网站上显示我的内容。

I have created a code snippet which can be copied and then put in any website. It shows my content on other websites.

我现在使用的是:

<script type='text/javascript'>
var user = 'abc';
var age = '23';

document.write('<iframe src="http://www.mysite.com/page.php?user='+ user + '&age=' + age + '" ></iframe');
</script>

在page.php中,

In page.php,

I根据用户年龄进行一些处理并显示动态内容。

I do some processing based on user and age and show dynamic content.

我的方法很好。

但是当我研究一些很好的标准方法来完成这些任务时,我发现了一种不同的方式。

But when I look into some good standard ways to do such tasks, I find a different way.

以google adsense代码为例。

Take an example of google adsense code.

<script type='text/javascript'>
 var a = 'somedata';
 var b = 'someotherdata';
</script>

<script type='text/javascript' src='http://www.google.com/adsenseurl.js'></script>

我想,因为 a b 是全球性的; adsenseurl.js必须使用它,最后他们可能会在iframe上显示它。

I guess, since a and b are global; adsenseurl.js must be using it and may be finally they are showing it on iframe.

所以,现在问题。

使用谷歌的方法有什么好处,我的做法有什么不对?

What's the advantage in using google's approach and whats wrong in my approach ?

ps我知道我应该尽量避免使用iframe,但我没有看到任何其他方法来实现这一点。

p.s. I know I should try to avoid using iframes but I dont see any other way to accomplish this.

推荐答案

你们之间的主要区别方法和adSense在我看来,你的代码必须放在广告必须出现的位置并加载像ads​​ense这样的脚本,他们可以在检查页面后将iframe放在DOM中 - 使用

The main difference between your approach and adSense is in my opinion that your code has to be placed where the ads have to appear and loading a script like adsense, they can place the iframe in the DOM after examining the page - using

var myIframe = document.createElement('iframe');
.
.
someDOMObject.appendChild(myIframe);

和/或操纵zindex将iframe浮动到页面上方

and/or manipulating the zindex to float the iframe above the page

最后,iFrame非常有用(无论你有没有哦,没有 - 你可能会听到iframe是邪恶的),因为你可以使用你喜欢的任何css和jquery。如果您所在的页面已经设置了div和旧版本的jQuery,那么您将需要做更多工作才能使其看起来像您想要的那样。

Lastly the iFrame is useful (regardless of "oh noes - iframes are evil" you may hear) since you can use any css and jquery you like. If the page you are on already has styled divs and old versions of jQuery you will have a lot more work to make it look like you want.

这篇关于iframe v / s外部js(在其他网站上显示我的网站内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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