To AJAX or not when need to deliver JavaScript files to another domain

Hi All. I have a situation. My website has user-generated content. I have just coded a widget to allow other sites to add this content. The widget uses iFrames and javascript. The javascript is for users of other sites to set parameters to decide which content they want, and to customize the widget look and feel.

So, there are three sections of code for site owners to add:

  1. In the head–a script tag with which loads all the js functions
  2. In the body–script tags with parameters and a call to a function in widget_head to set up the iFrames
  3. After the body–script tag with a call to a function in widget_head to load the iFrame src.

But, if I need/want to make a change to the js, how can I push the new js files out when they will be cached?

I know that adding a querystring to widget_head.js would do it. Instead of using widget_head.js as the src of the first script tag, I will use another calling script as the src. This calling script will simply determine what the querystring should be, and then use “document.write (widget_head.js?theQueryString)”

But how best to add the querystring. I figure there are two ways.

  1. In the calling script, determine the month and year and use it as the querystring. For example: widget_head.js?qs=201802. This way, every month, a new file will load. If I wan to do updates, I just need to know that they wont be live until the first of each month. If no updates, the uncached file is still sent. no big deal I dont think

  2. In the calling script, run AJAX to get the current version from my database. This way, the file only changes when I update my db with a new version. BUT, I am making an AJAX call every time.

Unless, there is another way to go, does anyone have any thoughts on this?

So how I generally handle is just to generate a completely random number to tack on the end.
“widget_head.js?widget=mywidget&r=”+Math.random()
Browser wont cache it (or at least, it’ll be incredibly unlikely to be cached)

It will increase calls as it forces a load of the file every time, but unless your webserver is particularly weak, your site is incredibly popular (read: Are you working for Google?), or the files being pulled are so large that you shouldn’t be pulling them anyway…

Then again, user generated script content makes me leery as …

I wanted to avoid pulling a file every time just for performance reasons–why do it if it isnt needed. But I guess that is pretty silly. The site is not popular where the amount of calls would matter to me. And, I guess even pulling the file each time is pretty meaningless performance-wise. Its about 20k.

Thats why I was thinking to make the querystring year + month. This way it will always use the cached version except for the first visit per month. When I make an update, it will simply go live only after a new month.

But, again, I guess it is pretty silly considering the little amount of traffic and the small size. I just always like to adhere to best practices for both coding and user experience and it would seem that not getting a new file would be the way to go IF it made a difference–which I am sure it would not. Right?

Will it make a difference? Yes. Will it make a NOTICEABLE difference?

To load this page to post on the forum, you loaded 21 javascript files from 2 different domains, of which 19 were external. If you notice the ones coming from that external domain, they’ve all got that random string on the end :wink:

If you’re comfortable with possible caching keeping things for a day/week/month/year/whatever, then go for it. Depends on how often your end-users want to write updates though, doesnt it?

Well, its not the end users that will write the updates, its all me. So, if/when I make a change to the widget, I would want to be able to send it out. The truth is, its unlikely to happen very often, at least not after the first few months. I would imagine that once it is stable and full-featured, there would not be much need for changes to the js. Thats why I would be ok with only having the update once in a while. But I guess updating every time would be just as good. Im a little indecisive on it, as you can see. But I think ajax is ruled out. there is no need for that. I need to add the querystring to change the file either every visit, every day, or every month.

Are you pulling a web page into an iframe when you could be retrieving a JSON string instead? This forum passes around JSON like … … .

Im not sure I understand the question. Yes, I am pulling a webpage into an iFrame. That is the content for the widget (I guess you knew that).

So, the way it is set up is pretty standard for widgets I assume:

The site owner adds a few script tags that I generate. One in the head (which is the one in question about how best to load), one or more in the body along with a div (this is where the user sets parameters and I generate an iFrame into the div), and then one at the end of the page (where the iFrame src gets populated).

I dont think I can use json for this (unless I am misunderstanding). json is for retrieving data only, right? And then, the site owner would need to use that data and format it properly.

What I am putting into the iFrame is actually part of an interactive page (like a user-created poll) so it has its own ajax and javascript associated with it.

So it works great as an iFrame, I am just struggling with the way to handle the main js script.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.