4

I used Jquery to insert a variable to div. Please check my code.

var msg = '<script src="https://gist.github.com/3010234.js?file=new.html.erb"></script>'
$('.result').html(msg);

msg variable contains a script that render a code snippet. msg is dynamic variable.

The above code is not working to insert code snippet to div.

Any Idea?

This script generate code snippet like this.

enter image description here

3
  • How are you calling the code? Document ready? Commented Jul 5, 2012 at 6:32
  • 2
    why are u adding script inside div??? Commented Jul 5, 2012 at 6:34
  • What is your purpose, execute a script or just display it? Commented Jul 5, 2012 at 6:43

4 Answers 4

2

To add script, I would add it to head like this;

var s = document.createElement('script');
s.src = 'https://gist.github.com/3010234.js?file=new.html.erb';
s.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(s);
Sign up to request clarification or add additional context in comments.

4 Comments

Not in this case. Adding a script to the head that outputs display code won't work, will it?
@BryanH: I know, just posted a common approach. Though in that case document.getElementById('id') would do that trick :)
var script = document.createElement("script"); script.src = "https://gist.github.com/3010234.js?file=new.html.erb"; script.type = "text/javascript"; document.getElementById('aaa').appendChild(script);. This code inserted script. but not render any code snippet.
@Shamithc: Since it is dynamically added, it will not be visible when you view Souce Code option from browser.
1

You cannot load a github gist into a page dynamically using that embed code. The embed code is fine if you can add the script tag to the HTML, but to do it dynamically via JavaScript as you are trying to do, it won't work because it relies on document.write().

Instead, use the github gists api:

$.get("https://api.github.com/gists/3010234", function(response) {
    $(".result").text(response.data.files["new.html.erb"].content);
}, "jsonp");

Working demo: http://jsfiddle.net/naTqe/

5 Comments

$.getScript("https://gist.github.com/3010234.js?file=new.html.erb"); is working. But I am not able to render code snippet generated by script into the div.
@Shamithc - The script your are trying to load uses document.write(), which you shouldn't use after the document has finished loading.
@Shamithc - I've abandoned my earlier answer, because, while technically accurate, it addresses a red herring and gets you no closer to accomplishing your actual goal of loading a github gist into a page dynamically using JavaScript. See my new answer above for a workable solution.
Yes, You are correct. But I missed gist styling, I need to generate code from that script itself. You are tried to get content only.
1
function loadScript() {
    var script = document.createElement("script");
    script.src = "https://gist.github.com/3010234.js?file=new.html.erb";
    script.type = "text/javascript";
    document.getElementById("result").appendChild(script);
}

5 Comments

There is no element with result as tag name in html :)
@Blaster yeh you are right, i updated. its better to include this in head tag
Change getElementsByID to getElementById :)
Since the OP is using jQuery, why not stick with it? jQuery's selectors are much easier (and safer) than getElementById.
@BryanH its easy, agreed. But how u can say that its more safer?
0

It looks like your remote JS is buggy. I ran it through JS Lint and came up with several errors.

Anyway, here is my working example. Keep a javascript console open and you'll see the error when it tries to parse the remote JS.

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <script type="text/javascript" src=
  "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <title>Jquery test</title>
</head>

<body>
  <h1>jQuery</h1>

  <p class="result">Some result</p>
  <script>
  /*<![CDATA[*/
  $(document).ready(function(){
  var msg = '<script type="text/javascript" src="http://gist.github.com/3010234.js?file=new.html.erb"><\/script>";
  $('.result').html(msg);
  });
  /*]]>*/
  </script>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.