Solved

jQuery insertAfter not working

Posted on 2014-01-07
4
727 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

810 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