Dan Hawkins Dan Hawkins - 7 months ago 11
Javascript Question

How to show a page by default on page load

I'm using jQuery to load pages into a div - instead of using iframes.

The code works great (feel free to reuse!) However, I have an issue where I cannot display a page when someone visits the URL.

So on page load, the canvas is empty (next to the menu) until the visitor clicks a link.

here is the code

<script>

$('[data-target]').click( function (e) {
$.get($(this).attr('href'), function(data){
$('#halloffame').empty();
$(data).find(".partner_body").appendTo("#halloffame");

});
e.preventDefault(); // prevent anchor from changing window.location
});
</script>


What I want to show is page-a.html whenever the page is visited - so the page doesn't look so empty.

Does anyone have any ideas?

Many thanks in advanced :)

Dan

Answer

So I managed to resolve this by using @Roberto's suggestion and my original code (Simply added load onto the end)

Here is the finished code, which loads a URL on page load (and still allows you to open links in a div)

  $('[data-target]').click( function (e) {
    $.get($(this).attr('href'), function(data){ 
     $('#halloffame').empty(); 
      $(data).find(".partner_body").appendTo("#halloffame");

    });
    e.preventDefault(); // prevent anchor from changing window.location
  }); 
  $.get( "pages/accounting-software/", function( data ) { 
       $('#halloffame').empty(); 
        $(data).find(".partner_body").appendTo("#halloffame");

   }); 

Hope this helps anyone else trying to achieve the same result.

Comments