Reputation: 28995
In my project,
I have created a code snippet which can be copied and then put in any website. It shows my content on other websites.
What I am using now is :
<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>
In page.php,
I do some processing based on user
and age
and show dynamic content.
My approach works fine.
But when I look into some good standard ways to do such tasks, I find a different way.
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>
I guess, since a
and b
are global; adsenseurl.js must be using it and may be finally they are showing it on iframe.
So, now the question.
What's the advantage in using google's approach and whats wrong in my approach ?
p.s. I know I should try to avoid using iframes but I dont see any other way to accomplish this.
Upvotes: 0
Views: 938
Reputation: 177685
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);
and/or manipulating the zindex to float the iframe above the page
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.
Upvotes: 2
Reputation: 9359
Why use an iframe? If your user loads the <script src='yoursite.com/something.js'></script>
, you immediately have access to his DOM and you can do whatever you want via that something.js
. In the same way that you load jQuery from some CDN - it can be immediately used to modify the DOM.
Otherwise there's really no difference except that:
There might be other advantages, but these two should suffice I believe.
Upvotes: 0