8

I've read several posts about this issue but i can't solve it.

I am loading an html file into a div. The file i am loading contains a unordered list. This list should be expanded (a menu with submenu items) and closed. Therefore i need js. But unfortunately this script isn't loaded.

Can anyone help me?

Would be so great! Thanks a lot :)

2
  • 10
    Show us some code... Commented Aug 22, 2009 at 21:30
  • Better answer from this post: stackoverflow.com/a/16352425/484358 Commented Oct 24, 2014 at 22:41

8 Answers 8

12

You want to load via AJAX into a div on your page, lets call it;

1) <div id="loadStuffHere"></div> of (abc.html)

2) the stuff to populate loadStuffHere comes from xyz.html

So just do this;

$("loadStuffHere").load("xyz.html");

BUT WAIT!! You dont want to have to load everything from xyz.html you just want to load a portion of xyz.html say <div id="loadMeOnly"></div> of (xyz.html)

So just do this;

$("loadStuffHere").load("xyz.html #loadMeOnly");

BUT WAIT!! Lets say inside of <div id="loadMeOnly"></div> is an accordion so which means it has to be initialized which means u have to also include the javascripts... hmm, what to do...

You would think of doing this;

$("loadStuffHere").load("xyz.html #loadMeOnly");
$.getScript('js/xyz.js');

Well the above sucks because a) u would need to create an external js file and b) You are actually making 2 http calls, when u could do it with 1 http call if you did it by normal non-ajax way.

The best solution is to get 2 things with 1 call the (HTML and the js - 1 line, 1 http) here is how u do it;

$("loadStuffHere").load("xyz.html #loadMeOnly, script");

This loads the #loadMeOnly div AND all script tags

The trick here is commas. You could pick and choose to load whatever doms you want

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

1 Comment

Except -- now that you have loaded that script, how to you run it?
2

So lets make sure you have jQuery loaded in the first place, be sure that the link to the library is in the head of your HTML, so something like this:

<script src="/javascripts/jquery-1.3.2.min.js" type="text/javascript"></script>

Use firebug to make sure it is loaded by checking the "net" tab.

Also be sure you are loading your javascript: (here I've called it "main")

<script src="/javascripts/main.js" type="text/javascript"></script>

Then in your js file do something like:

$.(document).load( function () {
  alert("loaded!")
});

That should fire the alert once the page has "fully loaded" I prefer to use ready() which will fire once the DOM is loaded.

If you have this all working and what you actually want to do is load and execute js from your js file try this:

.getScript()

This will load and execute the JavaScript you want.

4 Comments

Thanks! Where and how do i have to use this? is there no other possibility?
Well, I'm having trouble figuring out what you want to do, do you know for sure if jQuery is loaded on your page in the first place?
yes, my page works so far. jquery has to work to because of the lod() request. The only thing is the js in loaded content. I don't know how to solve that... :(
okay thanks jpsilvashy i'll test it and well be back in a minute :)
2

This solution works best. Using a callback function after the specific div or content container is loaded.

$('#CONTAINER').load('URL_TO_LOAD HTML_ELEMENT_TO_PLUCK_FROM_PAGE_AND_INSERT_INTO_CONTAINER', function () {
    $.getScript('PATH_TO_SCRIPT_THAT_YOU_REQUIRE_FOR_LOADED_CONTENT');
});

Comments

2

Juhu. I've solved it. Don't know wheather this is the most elegant way but it works :)

jpsilvashy your getscript(); has done it :) I've included these two lines in my content which should be loaded:

<script type="text/javascript"> 
$.getScript("js/tutorial-1.js");
$.getScript("js/jquery.ahover.js");
</script>

that works.

For everyone who has the same problem there's a small hint. You should delete the body and head tags. If they are included it doesn't work.

But now there's still a question: Why do i need to include this in the loaded content? I think the JavaScript has to be loaded after the content was loaded into the dom.

But that only some assumption!

Thanks for your great help!

1 Comment

does no one knows an explanation?
2

The script can't use document.ready, the document already ready. Just put the script between script tags.

wrong:

<script type="text/javascript">
   $(document).ready(function() { 
       alert($('div').html());
   });
</script>

correct:

<script type="text/javascript">
       alert($('div').html());
</script>

Comments

0

Ahh, I see... If you just want to load some html use:

$("selector for div ").load("/path_to_html.html div#main");

Where div#main is the div I want to retrieve in your remote file, but remember this must be the same domain for it to work.

6 Comments

that works, the menu you see is the loaded content. this menu should be folded together(the js makes that). and this script is not loaded. thats the problem :)
just to make it clear how it should look like. open johnlikesit.jo.ohost.de/helpme/htmlcontent.html i've included the js files in the second file to show you what i mean :)
even when the js files are included in both(the first file and the html file that should be loaded in the first one it doesn't work :(
so you shouldn't need any javascript in the file that is being loaded, we are only modifying the DOM so what is being inserted will still be under the influence of the js you have running under your original file, including all you events and everything. I'm not sure what you are ultimately trying to accomplish, is there anyway you can try to explain it a different way?
okay, i'll try to :) the part which should be inserted contains a menu which can be expanded. Without javascript this couldn't be done(as far as i know). So first i load the html content from the file "htmlcontent.html" into the div "container" in my "index_helpme.html" This works so far. The only problem is interacting with the loaded htmlcontent.html. If i want to change the css style with jquery or make some effects, it doesn't work. I hope this explanation is clear to understand :) Thanks for your help!
|
0

You can load javascript file dynamically with help of jQuery.getScript and set a callback function. When the script has been loaded the callback function called. Here's an example of loading highlightjs library from cdnjs:

$.getScript("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/highlight.min.js", function () {
    $('pre code').each(function(i, block) {
        // hljs now is available
        hljs.highlightBlock(block);
    });
});

Comments

-1

You need to explicitly call ahover on the unordered list. See the documentaton.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.