tags:

views:

70

answers:

1

Hello everyone, this is my first time really using this site. I'm relatively new to using ajax with my sites and I ran into a problem a little while ago. The thing is, I'm not sure what my problem is exactly because every time I went over my script, it made sense to me (and it fit with everything I looked up on Google and the jQuery website). Basically, my script doesn't work at all and I need to get it working somehow. If any of you could please help me, I would greatly appreciate it. Here's the code:

$(document).ready(function(){  
    $().ajaxSetup({cache: false});
    setInterval("checkAnchor()", 300);  
});  

//Function which check if there are anchor changes, if there are, sends the ajax petition 
var currentAnchor = null;  

function checkAnchor() {  
    //Check if it has changed
    if(currentAnchor != window.location.hash){  
        currentAnchor = window.location.hash;
        var hash = window.location.hash.substr(1);
        var newLink=$('a[href='+hash+']');
        var toLoad = hash+'.html #content';

        $('.current').removeClass('current');
        newLink.addClass('current');

        $('.box').slideUp(1500,function(){
            //Send the petition
            $('.box').load(toLoad,'');
        }); 
        $('#nav').append('<span id="load">LOADING...</span>');
        $('#load').fadeIn('normal');
        $('.box').slideDown(1500,function(){
            $('#load').fadeOut('normal');
            $('#load').remove();
        });
    });
}

And the html file:

<html>
 <head>
  <link rel="stylesheet" type="text/css" href="css/general.css" />
 </head>
 <body>
  <!--Nav Bar-->
  <div id="nav" class="center round">
   <ul>
    <li><a class="current" href="#home">Home</a> | </li>
    <li><a href="#upcomingevents">Upcoming Events</a> | </li>
    <li><a href="#attractions">Attractions</a> | </li>
    <li><a href="#facts">Facts</a> | </li>
    <li><a href="#placestostay">Places to Stay</a> | </li>
    <li><a href="#workscited">Works Cited</a></li>
   </ul>
  </div>


  <!--This is where content is loaded via ajax-->
  <div class="box center round">

  </div>

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
 <script type="text/javascript" src="scripts/ajax2.js"></script>
 </body>
</html> 
+1  A: 

I've modified your URLs so that they all have the id nav_link and a rel that should carry the url that you want to load.

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/general.css" />
    </head>
    <body>

    <!--Nav Bar-->
    <div id="nav" class="center round">
        <ul>
            <li><a href="home.html">            Home</a> | </li>
            <li><a href="upcomingevents.html">  Upcoming Events</a> | </li>
            <li><a href="attractions.html">     Attractions</a> | </li>
            <li><a href="facts.html">           Facts</a> | </li>
            <li><a href="placestostay.html">    Places to Stay</a> | </li>
            <li><a href="workscited.html">      Works Cited</a></li>
        </ul>
    </div>

    <a href="shall not trigger" >shall not trigger</a>




    <!--This is where content is loaded via ajax-->
    <div class="box center round"></div>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript" charset="utf-8">


        $(document).ready(function(){ 

            $('#nav a').click(function(){

                var the_url = $(this).attr("href");
                alert( the_url );
                $(".box").load( the_url );

                return false;
            });


        });  


    </script>


    </body>
</html>

As you can see, the jQuery code is fairly simple so it track's all clicked id's with nav_link and looks for the propper url and loads it to your box.

Hope it helps!

Herr Kaleun
really, the same id for all links? This is so wrong...
Ventus
so correct it:) It does work and for a beginner, the concept is very important. He could learn alot from this example
Herr Kaleun
I've "corrected" it, now it looks even better. :) Hope you like it too, @ventus ;)
Herr Kaleun
This is the only error I had on the console: Uncaught SyntaxError: Unexpected token }However, that error was resolved (Problem was solved, solution was in first comment by Felix Kling).Also, I wanted to make it so that if you clicked the back button in your browser, you would be taken to the previous page, even if you used ajax. I got it to work after I listened to what Felix Kling said: "Do you get any error on the console? The second to last line should be } instead of }); Maybe that already solves it."
Lobabob
Even though that did solve the loading and execution problems, I still have one specific problem left. That problem is that all the ' (apostrophes) characters show up weird for some reason....
Lobabob
why not accept my answer and make me happy?:)
Herr Kaleun