JordyvD
JordyvD

Reputation: 1615

Load segment of page with jquery and run code

Here's the situation: I've got a mainpage and i've got multiple partial pages, I use Jquery to .load these partial pages into the mainpage as navigation.

Example: $(mainpage).load("partpage .pageContent"; As you can see i don't load the full page, just a segment.

Here's the question: How do i run anything within the partial page's script tag? I've googled for the answer and haven't been able to find an answer. It does not run the code even when i put it into the segment i am loading at that moment.

Thanks, G3.

Upvotes: 0

Views: 738

Answers (1)

harlei vicente
harlei vicente

Reputation: 36

Well, so you have a main page and you want to load html into it as the user navigates. And these partial pages hava javascript that needs to be executed when included. Did i get that right ?

Here is the main page.

home.html

...
<body>

<script>

   //loading partial file via AJAX
   // would be triggered by a user clicking on a link
   var request = $.ajax({
      url: "segments/segment_a.html",
      type: "GET",
      dataType: "html"
   });

   request.done(function(partial_html){
      $('.page').html(partial_html)
   });

<div class='page'></div>

</script>

main page content

</body>

...

Now to the segment file in the server: /segments/segment_a.html

<div class='segment' id='segment_a'>

       <script>
          // script that should run when segment_a is loaded onto main page
       </script>

       <h2> Segment A </h2>

       <p> some text </p>

</div>

Once the segment page is loaded the javascript within the 'script' tags at the very top should be executed.

Upvotes: 2

Related Questions