English 中文(简体)
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, 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?

最佳回答

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.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签