0

I already asked some questions about ajax, but I still don't get it.

I took a script that I found on the internet and made some modifications, but it didn't work!

HTML:

<a data-toggle="team" id="times">TEAM</a>

ORIGINAL SCRIPT:

<script>
// THIS IS WHERE THE MAGIC HAPPENS
    $(function() {
        $('nav a').click(function(e) {
            $("#loading").show();
            href = $(this).attr("href");

            loadContent(href);

            // HISTORY.PUSHSTATE
            history.pushState('', 'New URL: '+href, href);
            e.preventDefault();


        });

        // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL
        window.onpopstate = function(event) {
            $("#loading").show();
            console.log("pathname: "+location.pathname);
            loadContent(location.pathname);
        };

    });

    function loadContent(url){
        // USES JQUERY TO LOAD THE CONTENT
        $.getJSON("content.php", {cid: url, format: 'json'}, function(json) {
                // THIS LOOP PUTS ALL THE CONTENT INTO THE RIGHT PLACES
                $.each(json, function(key, value){
                    $(key).html(value);
                });
                $("#loading").hide();
            });

        // THESE TWO LINES JUST MAKE SURE THAT THE NAV BAR REFLECTS THE CURRENT URL
        $('li').removeClass('current');
        $('a[href="'+url+'"]').parent().addClass('current');

    }

</script>

MODIFIED SCRIPT:

<script>

// THIS IS WHERE THE MAGIC HAPPENS
$(function() {
    $('#times').click(function(e) {
        $("#loading").show();
        var href = $(this).attr("data-toggle");

        loadContent(href);

        // HISTORY.PUSHSTATE
        history.pushState('', 'New URL: '+href, href);
        e.preventDefault();


    });

    // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL
    window.onpopstate = function(event) {
        $("#loading").show();
        console.log("pathname: "+location.pathname);
        loadContent(location.pathname);
    };

});

function loadContent(url){
    $.ajax({
        url: 'ajaxcontdent/ajax'+url,
        type: 'GET',
        error: function(){
            // always good to have an error handler with AJAX
        },
        success: function(data){
            $('#content').html(data);
        }

        // THESE TWO LINES JUST MAKE SURE THAT THE NAV BAR REFLECTS THE CURRENT URL
        $('li').removeClass('current');
        $('a[href="'+url+'"]').parent().addClass('current');

    };

</script>

What is wrong with my script? Nothing happens. I click in my <a> link and nothing. I already tried to put the file location on a hrefattribute, but then e.preventDefault(); doesn't work and my website runs like there is no AJAX.

In the original code, the author use some content.php file. But I don't know JSON, so I have no idea what did he put in that file.

There are no errors in the console.

My ajaxcontent/ajaxteam.php file content:

<p style="color:#fafafa;">Team</p>

It's just one line indeed. Just a test.

9
  • 1
    What this script doing? What is your actual issue? do add expected output. Commented Feb 19, 2016 at 5:52
  • Nothing happens. I click in my <a> link and nothing. Unless I put file location on hrefattribute, but then AJAX don't work. Commented Feb 19, 2016 at 5:53
  • how is your ajaxcontdent/ajaxteam look like? Commented Feb 19, 2016 at 5:54
  • 1
    what errors do you have in console? Commented Feb 19, 2016 at 5:54
  • Do provide complete html. In your script code I can see li tag as well that showing error to me Commented Feb 19, 2016 at 5:57

2 Answers 2

1

I think that may be a cause syntax error, I have regenerate one of your function so please use below code.

    function loadContent(url){
        $.ajax({
            url: 'ajaxcontdent/ajax'+url,
            type: 'GET',
            error: function(){
                // always good to have an error handler with AJAX
            },
            success: function(data){
                $('#content').html(data);
            }
        });
    };

Then use your operation, whatever you want, in your success block of above function.

I hope thin will help you.

Thansk

Sign up to request clarification or add additional context in comments.

Comments

0

In your code if I am not wrong

url: 'ajaxcontdent/ajax'+url,

this is the main culprit.

Try this one:

url: 'ajaxcontent/ajax'+url,

1 Comment

Actually, I put it there to get an error, but it was not working. Anyway, it was a brackets and parentesis issue Thank you, @devd c:

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.