Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed styling and updated test

  • Loading branch information...
commit a57370d03fb911acdb7e8db10437396ae9cb2efb 2 parents c00f2fc + c2ee87f
@blairvanderhoof blairvanderhoof authored
Showing with 18 additions and 25 deletions.
  1. +3 −3 example.html
  2. +15 −22 gist-embed.js
View
6 example.html
@@ -1,16 +1,16 @@
<!DOCTYPE html>
<html>
<head>
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="gist-embed.js"></script>
</head>
<body>
<code id="gist-1"></code>
<code id="gist-1000"></code>
- Loading all files
+ <p>Loading all files</p>
<code id="gist-4147951"></code>
- Loading a file at a time
+ <p>Loading a file at a time</p>
<code id="gist-4147951" data-file="file1.txt"></code>
<code>
View
37 gist-embed.js
@@ -9,7 +9,7 @@ $(function(){
$elem = $(this);
id = $elem.attr('id') || '';
- file = '?file='+$elem.attr('data-file') || '';
+ file = $elem.attr('data-file') ? '?file='+$elem.attr('data-file') : '';
//if the id doesn't begin with 'gist-', then ignore the code block
if (!id || id.indexOf('gist-') !== 0) return false;
@@ -26,31 +26,24 @@ $(function(){
//loading
$elem.html('Loading gist ' + url + ' ...');
//request the json version of this gist
- $.ajax({
- url: url,
- dataType: 'jsonp',
+ $.ajax({
+ url: url,
+ dataType: 'jsonp',
timeout: 10000,
success: function(response){
//the html payload is in the div property
if(response && response.div){
- //add the html to your element holder
- if(response.stylesheet){
- var css=/embed.css/;
- var style=false;
- $('link').each(function(){
- if(css.test($(this).attr('href'))){
- style=true;
- }
- });
- if(!style){
- var l = document.createElement("link");
- l.type = "text/css";
- l.rel = "stylesheet";
- l.href = response.stylesheet;
- var head = document.getElementsByTagName("head")[0];
- head.insertBefore(l, head.firstChild);
- }
+ //add the stylesheet if it does not exist
+ if(response.stylesheet && $('link[href="' + response.stylesheet + '"]').length === 0){
+ var l = document.createElement("link"),
+ head = document.getElementsByTagName("head")[0];
+
+ l.type = "text/css";
+ l.rel = "stylesheet";
+ l.href = response.stylesheet;
+ head.insertBefore(l, head.firstChild);
}
+ //add the html to your element holder
$elem.html(response.div);
}else{
$elem.html('Failed loading gist ' + url);
@@ -59,7 +52,7 @@ $(function(){
error: function(){
$elem.html('Failed loading gist ' + url);
}
- });
+ });
}else{
$elem.html('Failed loading gist with incorrect id format: ' + $elem.attr('id'));
}
Please sign in to comment.
Something went wrong with that request. Please try again.