Solved

jQuery insertAfter not working

Posted on 2014-01-07
4
704 Views
Last Modified: 2014-01-07
Hi,

I'm trying to insert some <script> tag after the jquery using:

<script id="jquery" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

$(document).ready(function() {
$( "<script src='https://www.ultratrust.com/scripts/plugins/iview/test.js'></script>" ).insertAfter( "#jquery" );
});

Open in new window


But I can see in the Google Dev tool that it's failing. What am I doing wrong?
0
Comment
Question by:Victor Kimura
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 250 total points
ID: 39763894
Don't include the closing script tag.

$(document).ready(function() {
$( "<script src='https://www.ultratrust.com/scripts/plugins/iview/test.js'>" ).insertAfter( "#jquery" );
});
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 39763911
Do you really need to care about where you append it?
Because if your code run, that mean jQuery is available.
http://api.jquery.com/jquery.getscript/

Check this :
<script id="jquery" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
   jQuery(function($) {
        $.getScript("https://www.ultratrust.com/scripts/plugins/iview/test.js");
   });
</script>

Open in new window

0
 

Author Closing Comment

by:Victor Kimura
ID: 39763930
Hi Tom and leakim! Thanks. Both work.

Yes, I see your point, leakim. I guess it doesn't matter since it's async js. =)
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39763954
Additional info for future reference.

@leakim971's solution is probably more reliable. Mine is more of a hack to trick jquery into including the script block. Normally, jquery prevents script blocks from being added to the DOM in this way because they could cause infinite loops of code execution or because they could be inserted multiple times. The difference between the two solutions is that mine will actually cause the script block to appear in the DOM whereas @leakim's will not which makes debugging difficult. A third option would be plain old javascript:
<script>
$(document).ready(function()
{
    var testJs = document.createElement("script");
    testJs.type = "text/javascript";
    testJs.src = "https://www.ultratrust.com/scripts/plugins/iview/test.js";
    $("head").append(testJs);
});
</script>
Placement of this script in the head of the page becomes important.

Thanks for the points.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now