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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:50:06+00:00 2026-05-26T15:50:06+00:00

Is it possible what I have mentioned in the title? I would like to

  • 0

Is it possible what I have mentioned in the title?

I would like to use this for checking existence of an element like so:

if($('#item')){...}

Any ideas?

That’s the code where I use it:

if($('#auto_redirect_in_3_s').length)//I "wished" $('#auto_redirect_in_3_s')
    {
        var timer = setTimeout("document.location.href = index_url;",3000);
    }

description:
If I put in php code it means that the page have to redirect in 3 s.

  • 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-05-26T15:50:07+00:00Added an answer on May 26, 2026 at 3:50 pm

    No. The jQuery factory function (aliased as $) returns an instance of a jQuery collection object–even when the selection didn’t match anything. If the selector did not match, the collection is empty, but it’s still an object so it evaluates to true. You must check .length to see if you got anything.

    Such checks for .length, however, are usually unnecessary given how jQuery works and are often a sign of poor logic or a misunderstanding of jQuery. So if you post your code, we can probably help clean it up.

    Update:

    Ok, your updated question provided the following code:

    if( $( '#auto_redirect_in_3_s' ).length ) //I "wished" $('#auto_redirect_in_3_s')
    {
        var timer = setTimeout( 'document.location.href = index_url;' ,3000 );
    }
    

    What you’re doing isn’t terrible and could be considered an Ok usage of .length. Allow me, however, to show you a few other ways of accomplishing the same thing. I am not saying you should do any of the following, just showing there are multiple ways to skin a cat, and hopefully show you a little more jQuery usage in some of the examples.

    If you’re stuck redirecting a page from the client side instead of with an HTTP header, the best way to accomplish it is with a meta tag:

    <meta http-equiv="refresh" content="3; url=http://example.com/">
    

    If you desire to keep it in JS you could avoid doing it based on the presence of an element, but on the value held by some input:

    <input type="hidden" id="auto_redirect_do" value="1" />
    <input type="hidden" id="auto_redirect_url" value="http://example.com/" />
    <input type="hidden" id="auto_redirect_delay" value="3000" />
    
    <script type="text/javascript>
    $( function()
    {
        if( $( '#auto_redirect_do' ).val() === '1' )
        {
            window.setTimeout(
                function()
                {
                    window.location = $( '#auto_redirect_url' ).val();
                },
                $( '#auto_redirect_delay' ).val()
            );
        }
    } );
    </script>
    

    Or, with a JSON encoded value in an input (or a data attr on an element):

    <input type="hidden" id="auto_redirect" value="{&quot;do&quot;:true,&quot;url&quot;:&quot;http:\/\/www.example.com&quot;,&quot;delay&quot;:3000}" />
    
    <script type="text/javascript>
    $( function()
    {
        var auto_direct = JSON.parse( $( '#auto_redirect' ).val() );
    
        if( auto_direct.do === true )
        {
            window.setTimeout(
                function()
                {
                    window.location = auto_redirect.url;
                },
                auto_redirect.delay
            );
        }
    } );
    </script>
    

    Going back to your version of looking for the presence of an element, you could do the following if you wanted to avoid using .length and make use of jQuery’s chaining:

    $( '#auto_redirect_in_3_s' ).each( function()
    {
        /*
            We got in here because the element was present
        */
    
        window.setTimeout( function()
        {
            window.location = index_url;
        }, 3000 );
    
        /* 
            Return false from this function in case we matched more
            than one--no need to setup the redirection multiple times
        */
        return false;
    } );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how this can be achieved. Assume: That there's a
When using a mailto link like this: <A HREF=mailto:user@domain.dom TITLE=Subject>Link Text</A> is it possible
Within Java code it is possible to have numeric literals that use a suffix
It is theoretically possible to have a converter, that scans the color areas of
Is it possible to have growing files on amazon s3? That is, can i
Is it possible to have a piece of code or a function that does
Basically, what the title says. I have several properties that combine together to really
As the title indicates, I would like to do the following, but the compiler
I use visual studio 2008, and I would like to know if it is
How can I solve the problem I mentioned in the title? So I have

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.