[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

jQuery insertAfter not working

Posted on 2014-01-07
4
Medium Priority
?
807 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

656 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