Making an embeddable widget, with expanding content

Leros

Lifer
Jul 11, 2004
21,867
7
81
I'm working on a project which involves an embeddable widget that our customers can place on their sites. I'm using an iframe to embed the content with some jquery magic to resize the iframe to fit the content. This worked great in the beginning.

I've added some expanding content to the widget (using jquery's slideToggle function), but the iframe does not expand with the new content. What is the best way to go about embedding content that can expand in size?

(I have very little knowledge about web programming, so go easy on me).
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,835
4,815
75
Must you use an iframe? The general idea of an iframe is to separate the container from the contents, so the container page doesn't know the size of the contents of the iframe.

Would jQuery UI help?
 

Ka0t1x

Golden Member
Jan 23, 2004
1,724
0
71
You should be able to get away with using a regular div and populating the div via innerHTML/.html();
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
Must you use an iframe? The general idea of an iframe is to separate the container from the contents, so the container page doesn't know the size of the contents of the iframe.

Would jQuery UI help?

I didn't know about jQuery UI. What a great resource!

As I understand, an iframe also prevents the client from modifying your widget with CSS or javascript. If you just put your content in a div or something, then the client can modify the content/styling of the widget, whether on purpose or accidentally.
 
Last edited:

Leros

Lifer
Jul 11, 2004
21,867
7
81
You should be able to get away with using a regular div and populating the div via innerHTML/.html();

So basically, my embeddable widget would be a piece of javascript? How exactly would that work?

Right now, the iframe just loads dynamic page (e.g. mysite.com/embed?id=123) off my server. I'm using Java servlets with a Tomcat server if it matters.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
So how I can do this with Javascript? Forget the iFrame.

I'd like to just populate a div, which I could do with jquery's load function. But, that will not work cross site. It seems like it should be doable, since this is basically what Google Adsense does.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
If you can create the element with pure DOM functions and have something that can output the script, they can just reference your js in a script tag. You'd need to have the script have all necessary data for the widget, then create the element with the DOM.

Is this static content in the Widget? It's real hard to make suggestions without knowing exactly what you want the thing to do.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
I ended up using javascript instead of my iframe. My embed code looks something like this:
Code:
<script type="text/javascript"> 
	var mysite_embed_id = "123";
	//additional optional params
</script> 
<script src ="mysite.com/js/embed.js"></script>

In embed.js, I add a script tag to the DOM which has a src of mysite.com/embed?callback=processResponse&id=123. It does a get from my server to get the javascript (which is really just JSON wrapped with the callback function):
processResponse(<JSON>)

This calls a function in embed.js called processResponse which takes the JSON and constructs the HTML that gets inserted into the DOM.

Its a bit annoying having to generate the DOM elements in javascript, because the Java servlet I was using with my iframe was simpler.

My only concern now is that the users can screw up the widget with their own javascript and CSS, but maybe I shouldn't worry too much about that. I just don't want people seeing my company's widget affected by a user's CSS/javascript and thinking that my company wrote a crappy widget.
 
Last edited: