0

So I have the following Jquery calling a Javascript method

$j("a.Pinterest").attr("href", "javascript:void(addScript());");

And this is the Javascript Method

   function addScript() {
    var e = document.createElement('script');
    e.setAttribute('type', 'text/javascript');
    e.setAttribute('charset', 'UTF-8');
    e.setAttribute('src', 'http://assets.pinterest.com/js/pinmarklet.js?r=' + Math.random() * 99999999);
    return document.body.appendChild(e);
}

I was wondering if there was a way to have it all in Jquery ?

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
StevieB
  • 6,263
  • 38
  • 108
  • 193
  • 1
    Have a look at the jQuery documentation: http://api.jquery.com/jQuery.getScript/ – Felix Kling Apr 13 '12 at 15:47
  • hmm, writing some code now, pls hold on abit – A Person Apr 13 '12 at 15:47
  • Duplicates: http://stackoverflow.com/questions/4881164/loading-an-external-script-after-page-load-with-jquery, http://stackoverflow.com/questions/9441142/load-external-js-file-with-jquery, http://stackoverflow.com/questions/5624366/dynamically-load-external-js-and-write-response-to-a-div, http://stackoverflow.com/questions/4361033/jquery-load-script-on-click – Felix Kling Apr 13 '12 at 15:48
  • 1
    Instead of injecting inline JavaScript with jQuery, use a jQuery click handler. – Sparky Apr 13 '12 at 15:48

4 Answers4

1

http://api.jquery.com/jQuery.getScript/

$.getScript("http://assets.pinterest.com/js/pinmarklet.js")
    .done(function(script, textStatus) {
     console.log( textStatus );
 })

edit: I didn't try this, I just took your address and threw it in the getScript demo.

Rick
  • 1,563
  • 9
  • 11
1
$j("a.Pinterest").attr("href", "javascript:void(addScript());");

Please don't do this. I suggest using a click handler.

$j("a.Pinterest").click(addScript);

And then in your addScript you can use getScript to load the script.

function addScript(e) {
    e.preventDefault();
    $j.getScript('http://assets.pinterest.com/js/pinmarklet.js');
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0
function addScript() {
      return $('<script/>',{
                          'type':'text/javascript',
                          'charset':'UTF-8',
                          'src':'http://assets.pinterest.com/js/pinmarklet.js?r=' + Math.random() * 99999999 
              }).appendTo("body")[0];
}
Engineer
  • 47,849
  • 12
  • 88
  • 91
0

You can create elements in jQuery using the $ selector.

$('<script></script>')

jQuery is also chainable, so you can have this:

$('<script></script>')
    .attr({
        'type' : 'text/javascript',
        'charset', 'UTF-8',
        'src' : 'http://assets.pinterest.com/js/pinmarklet.js?r=' + Math.random() * 99999999)
     })
     .appendTo(whatever)

If you're doing conditional script loading, you might want to take a look at the yepnope library.

I would question why you're dynamically added a script to the page on a button click. Seems like you would want the script ready and waiting for the user to click. If you're worried about load time, but the script tag at the bottom of the page to defer the loading.

mccow002
  • 6,754
  • 3
  • 26
  • 36
  • I just wanted to hide the href attribute from the mark up, since the mark up has to go into content editor for a cms and I didnt want users to mess with it – StevieB Apr 13 '12 at 15:54