?
Solved

jQuery insertAfter not working

Posted on 2014-01-07
4
Medium Priority
?
782 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

765 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