0

I have this code in index.html file:

$(function(){
    $('a#link').live('click', function(e){ 
        $("div#element").load("file.html");
    });
});

In the file.html I have this code at the top

<script src="file.js" type="javascript"></script>

When I click on the link, the file loads fine, however the (according to firebug) the JavaScript file doesn't load. Does anyone know why? I tried put the script tags in the index.html file aswell, but it doesn't work like that.

4
  • What do you mean by "it doesn't work like that"? Isn't it just because the link to your js file is broken? Commented Dec 11, 2011 at 23:22
  • The link is probably fine. The problem is with the bogus "type" attribute value. There's really no point in using "type" unless you're explicitly trying to prevent script execution. Commented Dec 11, 2011 at 23:29
  • Link is fine, I removed the type as well, no differentce :( Commented Dec 12, 2011 at 0:02
  • @PeterStuart the script URL - is it really a relative URL? If so, understand that it will be interpreted relative to the URL of the main page, and not the URL from the ".load()". Just guessing. Commented Dec 12, 2011 at 13:36

4 Answers 4

3

Try loading the page scripts after the page is loaded in your div as follow:

$(function(){
    $('a#link').live('click', function(e){ 
        $("div#element").load("file.html", function(){
            $.getScript("file.js", function(){
               // second page's scripts are loaded...
            });
        });
    });
});

A simpler way to fix this, as Pointy stated, is either to fix the type attribute or omit it.

Sign up to request clarification or add additional context in comments.

5 Comments

Not necessary if the "type" attribute on that <script> tag is simply corrected from the (invalid, and liable to be ignored by a browser anyway) "javascript" to "text/javascript" or, better, just left off entirely.
Also this requires that the loading code know the name of the script to load. What if the contents change?
The question was clear. No one said anything about the content changing. Stop assuming :p
I used the moethod, the files are loading, however the javascript files aren't responding to the file.html pahe, if that makes sense?
@Pointy I totally agree with you. However this is a specific question, that has very specific scope. If the question had begun with "I have X pages.." i would have thought of something a bit more dynamic...
2

if you need to load scripts, you can use getScript method instead

1 Comment

This is not true. When the payload returned from ".load()" has script tags in it, then jQuery finds them and runs them (if they've got the correct type).
0

When loading in html with javascript you normally need to remove the html head and body tags to make the javascript fire. I think this is also mentioned here jQuery .load() call doesn't execute javascript in loaded html file

Comments

0

The problem is that when you ".load()" scripts like that, jQuery will only run them if there's no "type" attribute, or if the "type" attribute has the precise value "text/javascript". Anything else and it's completely ignored.

Your script has just "javascript" for that attribute, so it's thrown out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.