2

I know this has been asked before and I tried to do what was suggested but I can't seem to get it working.

Basically I've got links like this:

<ul>
    <li class='current'><a href='portfolio/index.html'>portfolio</a></li>
    <li><a href='about/index.html'>about</a></li>
</ul>

And then I've got a div like this:

<div class='loaded_content'></div>

What I want to do is, load the page in the link href into the div as I press the link. I also want to prevent the browser from navigating the link the normal way.

This is the script I have, which is loaded in the html header along with the jquery base:

$(document).ready(function() {
    $('a').click(function(event){
        event.preventDefault();
        $('.loaded_content').load($(this).attr('href'));
        return false;
    });
});

What am I doing wrong?

edit:

Ok so I placed the script into the document.ready and the preventdefault is now working, but the href is still not getting loaded into the div.

edit:

Okay so I have got this working partially now. The html is now getting loaded properly into the div but I am now facing a new problem. The page I am loading is in a subdirectory along with its own .css file and some images. How can I make sure the css is being loaded aswell?

The reason I want to do this is because my pages on the website has the same structure on all pages, and I want to have like a frame website, where I only load the content of each page to keep from loading the same things over and over. Maybe there's another, better approach of this?

7 Answers7

3

You can use event.preventDefault(); to prevent the default behaviour of the link:

$('a').click(function(event){
    event.preventDefault();
    $('.loaded_content').load($(this).attr('href'));
});

Ensure that this code is either wrapped in $(document).ready(); or put it at the bottom of your html, otherwise the click event can not be attached to the links, because they need to be processed first.

Mario A
  • 3,286
  • 1
  • 17
  • 22
3

I think the problem is that the function isn't being called.

It can happen if you try to bind function to some elements when these elements are not yet in the document. You said that this code is the header, so, I think it is the case.

There are at least two options:

First: Write your javascript code inside a document.ready function:

$(document).ready(function(){
   // your on click function here
   $('a').click(function(){
       $('.loaded_content').load($(this).attr('href'));
       return false;
   });
});

Second: Move your javascript code, writing it after the links.

  • I just tried this. So I put the script into a document ready and now I know the function is being called because it no longer navigates the link to the href. However, the href doesn't seem to get loaded into the div. –  Mar 25 '15 at 23:09
  • The page is being redirected ? – Hilder Vitor Lima Pereira Mar 25 '15 at 23:10
  • No no, the preventdefault is working, but the href page isn't getting loaded into the .loaded_content div. –  Mar 25 '15 at 23:12
  • XMLHttpRequest cannot load file:///E:/Coding/HTML/portfolio/index.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.jquery_1.8.3.js -What does this mean though? –  Mar 25 '15 at 23:15
  • Does this help? http://stackoverflow.com/questions/4819060/allow-google-chrome-to-use-xmlhttprequest-to-load-a-url-from-a-local-file – Hilder Vitor Lima Pereira Mar 25 '15 at 23:20
  • It does seem to help, but it looks like the css and images isn't getting loaded. The pages I'm trying to load are in subdirectories of this main page, and they have their own css and image files. –  Mar 25 '15 at 23:31
  • @Hellsten it seems like path problems. You must check what directory is being used as current directory in the relative paths. Maybe, the main page's directory is the one being used and the relative paths from it is not right. – Hilder Vitor Lima Pereira Mar 26 '15 at 00:09
  • CORS Policies are blocking the URLs ... it's very annoying! – Tormy Van Cool Oct 31 '18 at 10:53
2

Try preventdefault ()

$('a').click(function(e){
e.preventDefault ();
    $('.loaded_content').load($(this).attr('href'));
    return false;
});
Mark
  • 4,773
  • 8
  • 53
  • 91
1
$(document).on('click', 'a', function(e){
    e.preventDefault();
    $('.loaded_content').load($(this).attr('href'));
});

http://api.jquery.com/load/

TheAivis
  • 181
  • 1
  • 4
0

change link to this

<a href='javasctipy:;' data-link='portfolio/index.html'>portfolio</a>

and then

$('a').click(function(){
  document.getElementById("loaded_content").innerHTML='<object type="text/html" data="'+$(this).attr('data-link')+'" ></object>';
});
miszczu
  • 1,179
  • 4
  • 19
  • 39
0

Add the jQuery library to the doc. Based on the fiddle, u have missed that.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mark
  • 4,773
  • 8
  • 53
  • 91
0

Open the file via your web server, eg

http://localhost/someFile.html

Chrome doesn't allow AJAX requests for 'file:///' URLs by default. Use the path with http://localhost/

Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28