Paparappa
Paparappa

Reputation: 2189

jquery history plugin not including script?

I'm using jquery.history lugin found here: http://tkyk.github.com/jquery-history-plugin/

With this i load content into a div. However the script (after analysing it with chrome webtools) does not include scripts. Say I have a div called #content. If i want to load page.html into it and page html contains a javascript code or include (as an example) like this:

<script type="text/javascript" src="jquery.js"></script>    
<script type="text/javascript">
    alert("hello world");
</script>

It won't load jquery.js or execute the alert. In webtools i instead see it like this:

You got it. Nothing. So it seems it excludes javascript somehow and only picks out the html. In my jqeury.history code i only load a certain div (.target) of the page.html and it looks like this:

(function($){
        var origContent = "";

        function loadContent(hash) {

            $('#content').stop(true,true).fadeOut();
            if(hash != "") {

                if(origContent == "") {
                    origContent = $('#content').html();
                }
                $('#content').load(hash +".html .target",
                                   function(){  $(this).prettyPrint();  });
            } else if(origContent != "") {
                $('#content').html(origContent);
            }
            $('#content').stop(true,true).fadeIn("fast");
        }

        $(document).ready(function() {
                $.history.init(loadContent);

                $('#navigation a').not('.external-link').click(function(e) {

                        var url = $(this).data('name');
                        url = url.replace(/^.*#/, '');
                        $.history.load(url);
                        return false;

                    });
            });

    })(jQuery);

Best regards!

Upvotes: 1

Views: 188

Answers (1)

Pointy
Pointy

Reputation: 413826

The jQuery ".load()" method strips out <script> elements and does not run them when the URL includes a selector.

            $('#content').load(hash +".html .target",
                               function(){  $(this).prettyPrint();  });

See how that URL has the ".target" selector after the actual URL (and a separating space)? That's what triggers the behavior.

I logged a bug about this some time ago, and the conclusion was to update the documentation. I think that the reasoning behind the scripts being dumped is that when ".load()" is plucking out just a portion of the loaded content, it can't be sure that scripts won't depend on other scripts outside the selected subset.

Upvotes: 1

Related Questions