3

I'm trying to call a function called loadPage on click of an a tag and pass it a URL to replace the current content in a div called main via Ajax.

However, my code isn't replacing the content already in the main div or even making the Ajax call as far as I can see and I can't see where it's going wrong.

JavaScript:

$(document).ready(function () {
    function loadPage(urlToLoad) {
        $.ajax({
            type: "POST",
            alert(urlToLoad);
            url: urlToLoad,
            data: dataString,
            success: function (returnedData) {
                $('#main').html(returnedData);
            }
        });
    }
});

HTML:

<nav>
    <ul>
        <li><a onclick="loadPage('load.php');" href="javascript:void(0)" class="nav"><img src="news.png" alt="Latest news" width="81" height="61" class="navImage" />Latest news</a>
        </li>
    </ul>
</nav>
<div id="main">
    <section id="mainContent">abc</section>
</div>
4
  • You're not executing the function anywhere.. Commented Mar 25, 2013 at 10:32
  • 1
    Please have a look at this article to learn how to debug JavaScript code. Commented Mar 25, 2013 at 10:35
  • 1) you have 'alert' in ajax-request parametrs - it's error 2)your function wrapped via anonimous function, so it out of global scope Commented Mar 25, 2013 at 10:40
  • If you are getting errors inside of the console (if you are using chrome, you can see them by pushing f12), then can you please paste the exact text so that we can provide more accurate answers. Commented Mar 25, 2013 at 10:43

5 Answers 5

3

You can simply do this using load:

function loadPage(urlToLoad) {
    $('#main').load(urlToLoad, function () {
        alert('Load was performed.');
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

why do you have alert inside ajax options.. this is throwing the error.. removing that and it should work..

//$(document).ready(function() { <--here
    function loadPage(urlToLoad) {
     alert(urlToLoad); // you can check that here...
      $.ajax({
            type: "POST",
           // alert(urlToLoad); <--here
            url: urlToLoad,
            data: dataString,
            success: function( returnedData ) {
             alert("success") //to check if ajax call is successfull
            $('#main').html( returnedData );
          }
        });

and no need to create a function inside document.ready()... keep it outside the ready function

3 Comments

Even when I move it outside the document.deady() and remove the alert it still doesn't work.
is that function called ?? if you alert something inside the function??
@Ben: We can only work with what you give us. We don't have psychic powers to remotely debug your code. I recommend to read the article I linked to and do some debugging on your own. Sorry if I sound harsh but "it does not work" is really not much to work with. The JavaScript code as it is looks "ok". We don't know if there is any other code that could cause problems or if your PHP script is correct. These are all things you have to check yourself.
0

You could do it in another way, by adding a custom "data-" attribute to your anchor tags, and using jquery to handle the click event.

$(document).ready(function()
{
   $('a.nav').on("click", function(event){
      event.preventDefault();
      $.ajax({
            type: "POST",
            url: $(this).attr('data-page'),
            data: dataString,
            success: function( returnedData ) {
            $('#main').html( returnedData );
          }
      });
   });
});


<nav>
    <ul>         
        <li><a data-page="load.php" class="nav"><img src="news.png" alt="Latest news" width="81" height="61" class="navImage" />Latest news</a></li>
    </ul>
</nav>

 <div id="main">
    <section id="mainContent">
        abc
    </section>
 </div>

Comments

0

Your function loadPage is scoped to the closure used for jQuery's document.ready. Later when the function loadPage is called, the call comes from the global scope. As a result, the function will not be accessible. Take your function out of $(document).ready(function(){});

Also note that when attempting to debug using alert (not really recommended, at the very least you should use console.log), that you need to put the alert on its own line of code, and not just in the middle of code like a breakpoint. Your alert should come before the ajax call.

function loadPage(urlToLoad) {
 alert(urlToLoad);
 $.ajax({/*...*/});
}

1 Comment

I moved it out of the document.deady() and it still doesn't work
0

Moveout the function from the doc ready handler:

function loadPage(urlToLoad) {
    $.ajax({
        type: "POST",
        url: urlToLoad,
        data: dataString,
        success: function (returnedData) {
            $('#main').html(returnedData);
        }
    });
}

$(document).ready(function () {
   $('nav a.nav').click(function(e){
      e.preventDefault();
      loadPage(this.href);
   });
});

try with this html:

 <nav>
   <ul>
     <li>
       <a href="load.php" class="nav">
          <img src="news.png" alt="Latest news" width="81" height="61" class="navImage" />Latest news
       </a>
     </li>
   </ul>
 </nav>

You should not alert in the ajax function properties, so remove the alert from there. It seems that you want it to be work on click of a certain link. So you can try this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.