Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:54:24+00:00 2026-06-17T21:54:24+00:00

Not entirely sure why this is going wrong. Jquery Novice. It should work, according

  • 0

Not entirely sure why this is going wrong. Jquery Novice. It should work, according to all the examples I’ve seen, but it doesn’t.

In the following script, the function sets new values for variables, but the new values don’t persist outside the function. (console logs show this)

$(document).ready(function() {

    // url
    var path = location.pathname.split("/"),
        pos1_path = path[1] != undefined ? path[1] : false,
        pos2_path = path[2] != undefined ? path[2] : false,
        pos3_path = path[3] != undefined ? path[3] :  false;

    //console.log('before: ' + pos2_path);
    if (pos1_path = 'article') {
        $.post("/js/ajax/article_crumbs.php", { func: "getArticleCrumbs" },
            function(data){
                pos2_path = data.category;
                //console.log('during: ' + pos2_path);
            return pos2_path}, "json");
    }
    //console.log('after: ' + pos2_path);

    // navigation
    // set category nav to current category
    if ( path[2] ) {
        $('#category_nav a[class$="' + pos2_path + '"]').toggleClass('current');
    }
    else {
        $('#category_nav a:first').toggleClass('current');
    }
});

The PHP:

<?php echo json_encode(["category"=>"arts-and-culture"]);

Also, according to the console printouts, the “after” print out appears before the “during”, does that mean the function doesn’t run until the end of the rest of the script?

EDIT: To clarify, I need to keep the { if(path[1]) setcurrent } part of the script out of the { if(pos1_path = ‘article’) } part so that if pos1_path is NOT article, it sets the value to whatever is in the URI..

SOLUTION as per answer below:

$(document).ready(function() {

    // url
    var path = location.pathname.split("/"),
        pos1_path = path[1] != undefined ? path[1] : false,
        pos2_path = path[2] != undefined ? path[2] : false,
        pos3_path = path[3] != undefined ? path[3] :  false;

    if (pos1_path = 'article') {
        //console.log('before: ' + pos2_path);
        var data = {id: pos2_path}
        $.ajax({
            type: 'POST',
            url: "/js/ajax/article_crumbs.php",
            data: data,
            success: function(data){
                pos2_path = data.category;
                //console.log('during: ' + pos2_path);},
            dataType: "json",
            async: false
        });
    }
    //console.log('after: ' + pos2_path);

    // navigation
    // set category nav to current category
    if ( path[2] ) {
        $('#category_nav a[class$="' + pos2_path + '"]').toggleClass('current');
    }
    else {
        $('#category_nav a:first').toggleClass('current');
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:54:25+00:00Added an answer on June 17, 2026 at 9:54 pm

    $.post request is asynchronous call so obviously it will move on to rest of the lines of code and not wait for callback of $.post

    Try using $.ajax instead and make it async=false to use same code

    $.ajax({
      type: 'POST',
      url: url,
      data: data,
      success: success,
      dataType: dataType,
      async:false
    });
    

    OR

    Move your code inside success callback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not entirely sure what's going on with this script, but basically this is
I'm not entirely sure what's breaking here on this page: http://grandoaks.org/floor.html but it seems
I've tagged this post as WordPress, but I'm not entirely sure it's WordPress-specific, so
I've tried searching for an answer to this problem, but I'm not entirely sure
I'm not entirely sure if this is the right place for this question, but
Not entirely sure what I did to cause this but when I try to
Not entirely sure this is possible, but hoping to be pleasantly surprised. I have
I'm not entirely sure why this isn't working. But this piece of code does
I'm not entirely sure why this crash is happening and I'd like to get
I'm not entirely sure if this is the best way to word it, however,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.