How to create embeddable gadget with Javascript and PHP?
I'm programming in Javascript and PHP. I've created a simple website that allows to fill in a quizz and get result. Now I would like to give users opportunity to embed this quizz on their website. How to do this? It should work in some near manner like Google Gadgets, Adsense and that: that so when someone will put my html code (with JS?) on their websites, quiz开发者_如何学Pythonz will display and work on their site.
Do you know any tutorials about doing such things or tips for me how to start searching and learning about it?
The easiest way, which is also best performance- and security-wise, is to use <iframe>
. Just create small version of your page for embedding.
<script>
is loaded synchronously and gets access to site's cookies, so it's not a good solution for embedding.
If you just ask your users to embed <script>
in place where they want the gadget to be, you'll be able to generate markup with document.write
(easy, works in HTML only).
A better way is to ask users to invoke function from your script that inserts code into selected DOM node (using W3C DOM). This allows smarter webmasters to load script asynchronously. SWFObject is designed like that.
If you need to use PHP you could tell your server to parse a .js file as PHP:
<Files quiz.js>
ForceType application/x-httpd-php
</Files>
In the quiz.js
file, use PHP code to retrieve questions from a database or whatever. Then, set mime-type headers to Javascript and output Javascript code.
Though as porneL says, using an iframe is simpler. That's what Google Gadgets does.
精彩评论