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 8388071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:23:17+00:00 2026-06-09T18:23:17+00:00

I have this code in my JS file. If i remove the the if

  • 0

I have this code in my JS file. If i remove the the if else loops then i can get the hello as alert otherwise not.

Neither i am getting any error in firebug console

Basically in those loops i am just checking if the url contains few keywords so that ican chnage the css to selected

$(document).ready(function(){ 
    var userPage = /child\/(.+)/.exec(location.href)[1];        
    if( userPage.indexOf('show') != -1 )
        {$('.profile').addClass('selected');}           
    if( userPage.indexOf('workbook') != -1 )
    {
        $('.selected').removeClass('selected');
        $('.workbook').addClass('selected');
    }
    if( userPage.indexOf('gallery') != -1 )
    {
        $('.selected').removeClass('selected');
        $('.gallery').addClass('selected');
    }
    if( userPage.indexOf('mylist') != -1 )
    {
        $('.selected').removeClass('selected');
        $('.mylist').addClass('selected');
    }

    if( userPage.indexOf('Photos') != -1 )
    {
        $('.selected').removeClass('selected');
        $('.photos').addClass('selected');
    }

    if( userPage.indexOf('profile/list') != -1 )
    {
        $('.selected').removeClass('selected');
        $('.list').addClass('selected');
    }                   
    alert("hillo");
  • 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-09T18:23:18+00:00Added an answer on June 9, 2026 at 6:23 pm

    The way you’ve set it up the alert is not inside any if statement, so it should alert no matter what. If there is no alert you’re script has errors, and I spot at least two :

    You’re assuming the url contains the string child/, if it does not your script will halt and not work, next you’re assuming the output of that exec() is an array with at least two values, selecting the second value of that array, and if for some reason your url does not contain child/ there will be no second value in that array, and the script halts, but then again it already halted in the exec().

    The second error is that the document.ready function is’nt closed, thats probably just a typo.

    On the other hand you should figure out a way to do this more dynamically. You’re using the same class as the url string you are checking for, so for most of the cases you could just use it directly in the selector, cutting your function down to just a few lines, something like:

    $(document).ready(function() {
        var url = 'http://somesite.com/child/gallery'; //just a test url
    
        var userPage = url.indexOf('child/') != -1 ? /child\/(.+)/.exec(url)[1] : null;
    
        if (userPage) {
            console.log('has child in url');
            $('.selected').removeClass('selected');
            $('.'+userPage).addClass('selected');
    
        }else{
            console.log('not ok');
        }
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
I have this code in a xaml file now in a event handler code
I have this code to download a file, but on sourceforge.net sever there is
I have this code to move my uploaded file to a specific directory: if
I have this code that reads from XML file. It gets five strings (groupId,
I have this code to write to a file, it works perfect but I
I have this code which will include template.php file from inside each of these
I have this code that saves a pdf file. FileStream fs = new FileStream(SaveLocation,
I have this code in a .html.erb file: <script src=http://connect.facebook.net/en_US/all.js#xfbml=1></script> <script> FB.Event.subscribe('edge.create', function(response) {
I have this code in PHP writing views to a text-file and just increasing

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.