Embedding content - Use iFrame, JS/jQuery, or what? - javascript

This gets somewhat complex, so first, a little background:
We're redesigning a video platform that we use across internal applications and our customers use as well for video delivery. The goal is for our customers to be able to embed stuff easily to put it on their own sites, as you would expect. Thus far, we've given a one-line script tag to do this, like so, which drops a player on a page:
<script src="http://vod.nimbushd.com/player/player.ashx?mediaItemAssetId=8413adeb-6b15-4606-a771-637527539093&h=480&w=720" type="text/javascript"></script>
Sample Page with the script above
This is just an ASP.NET handler that returns some Javascript, which, in turn renders some HTML for the player.
That works well enough for a player, but now on to the redesign.
We're now targeting different players for different browser types, HTML5 fallback in the absence of Silverlight, and some other bells/whistles. One of our big feature requests is to be able to serve up a player with add'l video gallery, Disqus comments, Twitter/Facebook share/like buttons; basically, regardless of the philosophy that these are things that should live natively on the customer site, we need to be able to design more complex features and serve them up the easiest embed-style possible.
I guess one school of thought is that we almost need an iFrame that behaves like embedded content (no scrollbars, sizing, and so on).
We're tossing around ideas like more complex Javascript-rendering HTML that could get impossible to maintain pretty quickly, whether advanced features should just be Rich content (Silverlight/Flash), or if there's some in-between like a jQuery partial page refresh or something.
Hoping to learn from some folks who have already gone down this road - as an end-user, how would you expect to embed content, and as a content provider, how do you serve that content in embeddable form?

Personally I would prefer a one line script to include for this
<script src="..." type="text/javascript"
data-container="someId"
data-otherParam="..."
data-beforeLoad="someFunction"
...
></script>
Passing all the configuration options in as html5 data attributes would be the most convenient. It would also be great if we can pass in a function name that you would call to give the end-user even more control where necessary.
As for what your actual code does. I would recommend you have the script do all your logic and have it make JSONP calls to a webservice to get any data it needs. As for the HTML rendering I recommend you use any templating engine and reference a templating file directly loading them from your server either directly or over JSONP if neccesary.
As for embedding your actual data/html pick a <div> or create a new one and render partial templates to the children of that <div>.
Alternatively you go down the easy route and just ask the user to embed an <iframe>. This solution isn't as nice.

Related

Embedding GitHub-Pages content without using iframe

My VueJS application usually contains links to other pages (documentations) but it would be desirable to display the documentation content inside my page without the users having to leave for another page. Some of them are hosted on GitHub pages and although I could embed them into my app via iframe, I can't get rid of the feeling that there must be a more elegant way to display the information from the external documentation.
All I can think of is to write some kind of HTML parser on top of jssoup, however, this seems to be a very tightly coupled approach to the target page that could potentially require adjustments whenever the structure of the target page changes..
Is there any potential approach that I could consider to solve this issue?
Thanks!
I think this topic is obsolete. Turns out that one could use iFrames or crawl the contents from which both solutions aren't very pretty and infeasible in general.

Contact Form Widget, to make it use by another website

I have to create a Contact Form widget — a block of code that users can paste into any site(And i want it to use by my client websites) that will contain a small form, I read so many blogs and code I come up with the answer that Vanilla Js is best for my requirement, I want some refrence or blog or code to make it possible.
I have plenty of ideas for how to do this, but I'm curious to know if there's any generally-accepted method for including the styles, JavaScript, etc. The best I can come up with now is a block that fetches all the necessary styles and tags from an external URL.
This is a broad subject and the solution will depend on how much integration or isolation you'll want with the host page. If you want your contact form to always look the same, an iframe is indeed the right solution but has limitations. Popups from your form won't be able to escape the iframe. Vertical sizing can be an issue, you may want to look into Pym.js to deal with this.
Note that this doesn't mean you need to provide client websites with the HTML code for the iframe. You can just give them a script tag which points to a JavaScript program that is in charge of building the iframe.
If you want the form to integrate seamlessly with the client site, inheriting styles, then your script will need to inject your form into the page, either using the DOM API or using innerHTML. Then it will take some trial and error to figure what CSS properties you'll want to enforce and what can be inherited from the host page. You'll need to namespace your CSS classes and work with CSS specificity. It's not always simple and never completely bulletproof, so if you can stick with an iframe you'll be safer.
In both cases, in order to know where to inject your form, you can use the script tag itself as a reference, for instance by giving it a unique data attribute that your script will use to find its own script tag and insert the form below or underneath it.
For a complete coverage of the subject, I can highly recommend the book Third-Party JavaScript from Manning.

Web Development Best Practices - How to Support Javascript Disabled

What is the best thing to do when a user doesn't have JavaScript enabled? What is the best way to deliver content to that kind of user? What is the best way to keep a site readable by search engines?
I can think of two ways to achieve this, but do not know what is better (or if a 3rd option is better):
Rely on the meta-refresh tag to redirect users to a non-javascript version of site. Wrap the meta-refresh tag in a noscript tag so it will be ignored by those with javascript.
Rely on an iframe tag located within the body tag to deliver a non-javascript version of site. Wrap the iframe tag in a a noscript tag so it will be ignored by those with javascript.
I would also appreciate high-profile examples of the correct or incorrect way to do this.
--------- ADDITION TO QUESTION -----------
Here is an example of what I have done in the past to address this: http://photocontest.highpoint.edu/
I want to make sure there aren't better ways to do this.
You are talking about graceful degradation: Designing and making the site to work with javascript, then making the site still work with javascript turned off. The easiest thing to do is include the html "noscript" tag somewhere near the top of your page that gives a message saying that the site REQUIRES javascript or things won't work right. SO is a perfect example of this. Most of the buttons at the top of the screen run via javascript. Turn it off and you get a nice red banner and the drop down js effects are gone.
I prefer progressive enhancement development. Get the site working in it's entirety without javascript / flash / css3 / whatever, THEN enhance it bit by bit (still include the noscript tag) to improve the user experience. This ensures you have a fully working, readable website regardless if you're a disabled user with a screen reader or search engine, whilst providing a good user experience for users with newer browsers.
Bottom line: for any dynamically generated content (for example page elements generated via AJAX) there has to be a static page alternative where this content must be available via a standard link. If you are using javascript for tabbed content, then show all the content in a way that is consistent with the rest of the webpage.
An example is http://www.bbc.co.uk/news/ Turn off javascript and you have a full page of written content, pictures, links etc. Turn on javascript and you get scrolling news stories, tabbed content, scrolling pictures and so on.
I'm going to be naughty and post links to wikipedia:
Progressive Enhancement
Graceful Degredation
You have another option, just load the same page but make it work for noscript users (progressive enhancement/gracefull degradation).
A simple example:
You want to load content into a div with ajax, make an <a> tag linking to the full page with the new content (noscript behavior) and bind the <a> tag with jQuery to intercept clicks and load with ajax (script behavior).
$('a.ajax').click(function(){
var anchor = $(this);
$('#content').load(anchor.attr('href') + ' #content');
return false;
});
I'm not entirely sure if Progressive Enhancement is considered to be best practice these days but it's the approach I personally favour. In this case you write your server side code so that it functions like a standard web 1.0 web app (no JavaScript) to provide at least enough functionality for the system to work without JavaScript. You then start layering JavaScript functionality on top of this to make the system more user friendly. If done properly you should end up with a web app that at least provides enough functionality to be useful for non-JavaScript users.
A related process is known as Graceful Degradation, which works in a similar way but starts with the assumption that a user has JavaScript enabled and build in workarounds for cases where they don/t. This has a drawback, however, in that if you overlook something you can leave a non-JavaScript user without a fallback.
Progressive Enhancement example for a search page: Build your search page so that it normally just returns a HTML page of search results, but also add a flag that can be set via GET that when set, it returns XML or JSON instead. On the search page, include a script that does an AJAX request to the search page with the flag appended onto the query string and then replaces the main content of the page with the result of the AJAX call. JavaScript users get the benefit of AJAX but those without JavaScript still get a workable search page.
http://en.wikipedia.org/wiki/Progressive_enhancement
If your application must have javascript to function then there's nothing you can do except show them a polite message in a noscript tag.
Otherwise, you should be thinking the other way around.
Build your site without JS
Give awesome user experience and make it full functional
Add JS and make the UX even more functional. Layer the JS on top.
So if the user doesn't have JS, your site will still revert to step two of your site state.
As for crawling. If your site depends on AJAX and a lot of JS to work, you can make gogole aware of it : http://code.google.com/web/ajaxcrawling/docs/getting-started.html
One quick tip that may help you: just install lynx, a command-line web browser, and you'll immediately see how google and other seo see your site (and blind people too). This is very useful. Of course, in a command line windows, there's no graphics and javascript is disabled.
If you're doing "serious" Ajax (e.g. client side-routing) the following technique could be useful:
Use Urls without GET/"?"-parameters (it makes your life easier later on)
Use http://baseurl.com/#!/path/to/resource for client side-routing
Implement rendering of non-script HTML-version of your site (HTML snapshot is what Google calls it) at http://baseurl.com/path/to/resource
Wrap the whole content of your HTML snapshot in noscript-tags and redirect via top.location.href to the full version of the site
Handle http://baseurl.com/?_escaped_fragment=/path/to/resource - it should redirect via 301-response to http://baseurl.com/path/to/resource
Use a-tags only for GET-links, use forms for POST/PUT/DELETE-links - unstyle the hell out of them if necessary
A nice example code for links I found while researching "How to write proper Ajax-code":
Resource
This is of course a pretty complex solution but it should enable both SEO (including non-search engine crawlers) and accessibility. The problem is that you have to be able to render your page server- AND client side.
One solution could be to use a templating framework like mustache where implementations for different platforms exist.
Use something like {{#pagelet}}/path/to/partial{{/pagelet}} for dynamic parts of your page - example: {{#pagelet}}/image/{{image_id}}/preview{{/pagelet}}
In your client-side rendering, pagelet would be implemented to be dynamically replaced with something loaded via Ajax (for example: render )
In your server-side rendering, pagelet would just be rendered directly (in doubt just curl the pagelet and render it right away - or if you can write the code asynchronously do it just as you would do it client side: write some temporary span into a buffer, start fetching all the pagelets, replace the temporary spans as the pagelets arrive and flush the buffer once all pagelets have been rendered.
That's the best general design I found so far. You can deep link into your app, it's search engine friendly and it should force you to build a page that gracefully degrades.
P.S.: One advantage of the techniques described above is that both the Ajax- and the "Web 1.0"-rendering of a page could profit from memcached-caching of whole pagelets.
I would prefer to code the page without javascript and then if javascript is enabled, we redirect users to a similar page with javascript. (same concept as progressive enhancement)
redirecting with javascript

How does social plugin work?

Introduction
I am planning to create a widget much like, Facebook provides for likes, share etc. Basically the requirement is:
The client will include JavaScript provided by us.
The client can, now, put our widgets in the page having the JS. The widget placeholder looks like <nw:fav id="911"/>.
When page gets loaded, the JavaScript replaces the placeholders with actual HTML. The JavaScript also keeps this DIV updated with dynamic (perhaps using long polling) content.
Problem
I am not sure how to do step #2.
How JavaScript will get all the <nw:... tags and all the attributes of it?
How to replace the tag with real HTML tag?
Constraints
I am trying to avoid JQuery or any other JS library, since we may cause conflict if the client is also using JQuery, may be some other version.
I don't really want to use IFrame.
I am looking for approaches. Open for suggestions.
The facebook connect JS SDK is open source. You might find the xfbml.js file useful. See this link.
Basically when the document loads, the code walks the dom looking for elements that have a specific namespace (FB uses fb, you appear to want to use nw). Find those elements and then replace them with whatever content you need.

How to setup a dynamic website with javascript only (no serverside)

Here's my problem: I want to build a website, mostly static but with some dynamic parts (a little blog for news, etc..).
My webserver can only do static files (it's actually a public dropbox directory!) but I don't want to repeat the layout in every html page!
Now, I see two possible solutions here: either I create an index.htm page that emulates site navigation with javascript and AJAX or I create all the different html pages and then somehow import the layout bits with javascript..
From you I need ideas and suggestions on how to implement this, which libraries to use, or maybe there exists even something tailored exactly for what I need?
Thanks!!
I would define the site layout in your index.html file, and then use JavaScript and Ajax to load the actual content into a content div on the page. That way your content files (fetched by Ajax) will be more or less plain HTML, with CSS classes defined in index.html. Also, I wouldn't recommend building a blog in pure HTML and JavaScript. It wouldn't be very interactive; no comments, ratings, etc. You could store your blog content in XML and then fetch and display it with Ajax and JavaScript, however.
While on the subject of XML, you could implement all your site content in XML. You should also store the list of pages (for generating navigation) as XML.
Just another one way. You can generate static HTML in your computer and upload result to dropbox. Look at emacs muse.
jQuery allows you to easily load a section of one page into another page. I recommend loading common navigation sections into the different pages, rather than the other way around to avoid back/forward problems. Layout can be done with a separate CSS file rather than with tables to minimize the amount of repeated code. For the blog, you could put each blog entry in a separate file and load each section individually.
However, I would just use something already available. TiddlyWiki, for example, is a self-contained wiki that is all in one file. It's very customizable, and there's already a blog plug-in available for it. You can work on the site on your hard drive or USB drive, and then you can upload it to the web when done. There's nothing more to it.
Have you considered using publishing software on your computer to combine your content with a template, resulting in a set of static pages that you can then upload to the dropbox?
Some options in this regard come to mind:
Movable Type - can output static HTML which can then be uploaded to the server
Adobe Dreamweaver
Apple iWork Pages
To handle comments, you can use Disqus. It inserts a complete comment system into your site using just JavaScript.
You can use the Google Closure templates. It's one of the fastest and most versatile javascript templating solutions around.

Categories