3

I thought that I could just use jQuery's .append() and add them to the head, but that doesn't seem to be working for my external scripts (Knockout.js).

Here's my code that runs when the page loads. It seems to be working for the stylesheet, but not for the external scripts.

if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.8.0') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "http://code.jquery.com/jquery-1.8.0.min.js");
    if (script_tag.readyState) {
      script_tag.onreadystatechange = function () { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
              scriptLoadHandler();
          }
      };
    } else {
      script_tag.onload = scriptLoadHandler;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

function main() { 
        jQuery(document).ready(function($) {
            $("head").append("<script type='text/javascript' src='http://knockoutjs.com/js/jquery.tmpl.js'></script>");
            $("head").append("<script type='text/javascript' src='http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-1.2.1.js'></script>");
            $("head").append("<link href='style.css' rel='stylesheet' type='text/css' />");

            // Then it appends the necessary HTML code [...]
        });
    }

Here's my test environment where you can see my current code in action with Firebug.

Here's what I'm seeing in Firebug after the page loads:

enter image description here

EDIT: It looks like it's having issues with the Knockout.js scripts in my code, so I'll look into those. Thank you for the comments and answer regarding dynamic scripts. I learned something :)

3
  • Have you tried appending it to the body? Commented Nov 19, 2012 at 17:54
  • That didn't seem to work, either. Commented Nov 19, 2012 at 17:55
  • Check this answer to a related question. I don't know why it would be different, appending through jQuery or using vanilla JavaScript, but apparently it is. I've had success with the latter, never actually tried the former... Commented Nov 19, 2012 at 17:59

4 Answers 4

8

Have you tried jQuery.getScript() ? It basically loads a script from the server and then executes it.

 $.getScript("yourScript.js", function(){});
Sign up to request clarification or add additional context in comments.

8 Comments

Is 'yourScript.js' the external ones in this case?
Yes, you can load external ones.
Hmm, this still doesn't seem to be doing it. Maybe I have an error somewhere else in my code.
You probably won't see it in the document's <head>, but I believe you can use it, can't you?
I suppose it could still be there, but it's just not showing up in the head. It's not making it through my Knockout.js code, so there might be an error elsewhere. I'll look into it.
|
1

Try to add scripts this way, I have seen that issue before in some browsers.

var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = script_url;
$("head").append( script ); 

Comments

0

According to this jQuery API pages comment here, this behavior is perfectly normal, as jQuery cleans up the DOM after you

Your code is executed (if URL is correct, and XSS is not blocked) whether you fetch it by $.append()ing it or use $.getScript().

However, loading your site gives me at least three two solid errors. You might want to work on those.

The errors:

ReferenceError: $ is not defined
search.js
Line 54

and

TypeError: jQuery is undefined
http://knockoutjs.com/js/jquery.tmpl.js?_=1353351345859
Line 7

2 Comments

Errors? I put in three alerts to see how far the code was getting. That could've been it.
@Jon I updated the answer with the errors, though I think it's odd that you don't see them.
-1

You should use something like AngularJS if your application is this complex. Otherwise you are reinventing the wheel.

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.