bootstrap-linkpreview.js is a JavaScript library offering a facebook-like preview for URLs. It is very simple to use and weighs just a few kilobytes.
You may want to try this library locally from a HTML file but you will face this issue:
XMLHttpRequest cannot load *…* Origin : * is not allowed by Access-Control-Allow-Origin.
This is due to the Same origin policy concept. Ajax calls can only be achieved with a URL based on the same domain they are launched on. That’s why you usually want to process link previews on your server. But sometimes you don’t have any other way than doing it on the client’s side.
Anyway don’t panic, this library is working perfectly with some conditions.
The best way to know how to use this library is to go through the examples in the folder demos
. There you find two ways to get around the same origin policy.
This example demonstrates how to use the library from a website. It simply filters every jQuery ajax calls and echoes it on the server. That way the ajax call get the results back. To run the code, simply upload the demo on your server or run a local Apache server.
Thanks to this thread for the php script.
The advantage with Google Chrome extensions is that it gives a way to bypass the same origin policy. To run it, follow these steps.
Call the library via javascript:
$('.element').linkpreview()
$('.element') can point to <input>
, <textarea>
or <a>
. Without any parameters, this will load the URL(s) and generate the preview just after the element(s).
There are a few options available for this library, e.g:
$('.element').linkpreview({
previewContainer: "#preview-container",
refreshButton: "#refresh-button",
onSuccess: function(data) {
console.log("Winner!");
}
})
Name | Type | Default | Description |
---|---|---|---|
url | URL | $element.val() || $element.attr('href') | URL to use instead of the element's content |
previewContainer | Selector | $element.after() | Container block receiving the preview of the link |
previewContainerClass | String | row-fluid well | Class of the previewContainer |
refreshButton | Selector | Button refreshing the preview | |
errorMessage | String | We are sorry we couldn't load the preview. The URL is invalid. | Custom error message |
preProcess | Function | Treatment to make before we load the link | |
onSuccess | Function | Callback when the data is loaded | |
onError | Function | Callback when an error occurred while processing the preview | |
onComplete | Function | Callback when a result has been received |
All dependencies can be found under the folder deps
:
Copyright 2013 Ekito - http://www.ekito.fr/
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.