Cam
Cam

Reputation: 1902

HTML inside of jquery XML

My problem is that I can't dynamically create <script>. Does anyone have a way of fixing this? I figure this because I'm attempting to do something that XML is not initially able to do. I wonder though if my XML needs to change maybe I need to use the html call somewhere instead thanks for the help.

$(document).ready(function(){
    $.get('js.xml', function(data){

        $(data).find('info').each(function(){

            var $info = $(this);
            var links = $info.find("scp");


            var html = '<script type="text/javascript" src="' + links + '"></script>';

            $('body').append($(html));

        });

    });

});

---------------------!! ADDED JSFIDDLE -------------------------------

Jsfiddle

Upvotes: 2

Views: 92

Answers (4)

Andrew Rhyne
Andrew Rhyne

Reputation: 5090

You don't need to dynamically create a script tag. You just need to inject your dependencies. I'd recommend a framework like angular, but getScript works as well with jQuery. The problem with using something like getScript with dependency injection is that your code base won't end up being well structured and modular. You'll end up with tightly coupled code that will cause problems long term. I'd recommend using something like AngularJS to encourage loose coupling. This is a big concern when you are building software that is very dynamic.

Upvotes: 0

Sirwan Afifi
Sirwan Afifi

Reputation: 10824

try use $.getScript instead of append it to DOM , for example :

$(document).ready(function(){
    $.get('js.xml', function(data){

        $(data).find('info').each(function(){

            var $info = $(this);
            var links = $info.find("scp");


            $.getScript(link);

        });

    });

});

it seems that your xml file contain :

<location>
<info>
<scp>
<script type="text/javascript" src="https://www.printersmall.com/jqueryui/1.8.17/jquery.cookie.js"></script>
</scp>
</info>
</location>

your xml file should be like this :

<location>
<info>
<scp>
    www.printersmall.com/jqueryui/1.8.17/jquery.cookie.js
</scp>
</info>
</location>

Upvotes: 1

Musa
Musa

Reputation: 97672

links is an jQuery object, if the contents of the scp is the script source then you need to use .text() to get it.

var links = $info.find("scp").text();

Upvotes: 0

keyboardSmasher
keyboardSmasher

Reputation: 2811

I'm no expert, but I don't think data is the same as $(data)

Upvotes: 0

Related Questions