views:

180

answers:

2

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, quizz 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?

+2  A: 

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.

porneL
+1  A: 

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.

DisgruntledGoat
`<script src=….php>` would have worked too. File extensions are meaningless in HTTP, it's more important to send appropriate `Content-Type` header: `header('Content-Type:application/javascript');`
porneL
True, but I feel that `<script src="quiz.js">` is a bit nicer than `<script src="quiz.php">`. Don't forget this is for other users to embed on their site, who may get confused about the PHP extension.
DisgruntledGoat