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

  • Home
  • SEARCH
  • 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 3807984
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:04:36+00:00 2026-05-19T15:04:36+00:00

I am writing a small Chrome extension and in my background.html I have the

  • 0

I am writing a small Chrome extension
and in my background.html I have the following:

<script type="text/javascript" src="jquery.js"></script>
<script>

    var hash = '';
    var tab_id = -1;
    var block;

    tab_id = get_tab_id();
    //no myurl page is opened
    if(tab_id == -1)
    {
        chrome.tabs.create({'url': 'http://myurl', 'selected': false});
        tab_id = get_tab_id();
    }


function get_tab_id()
{
    var tab_id = -1;

    //find the needed page and get id
alert('ins0');
    // get the current window
    chrome.windows.getCurrent(function(win)
    {
    alert('ins1');
        // get an array of the tabs in the window
        chrome.tabs.getAllInWindow(win.id, function(tabs)
        {
    alert('ins2');
            for (i in tabs) // loop over the tabs
            {
    alert('ins3');
                // if the tab is not the selected one
                if (tabs[i].url == 'http://myurl')
                {
    alert('ins4');
                    //get tab id
                    tab_id = tabs[i].id;
                }
            }
        });
    });
alert('ins5');
    alert('tab_id: ' + tab_id);
alert('ins6');
    return tab_id;
}
</script>

The strange is that when I launch the extension – the order of alerts is the following:

ins0
ins5
ins1
tab_id: -1
ins2
ins3
ins6

So it looks like it is jumping from one part of the code to the other.
Any ideas?

  • 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-19T15:04:37+00:00Added an answer on May 19, 2026 at 3:04 pm

    Chrome API calls are asynchronous, so if you want to execute them in order you need to use callbacks. If all you need is to get newly created tab id then:

    chrome.tabs.create({'url': 'http://myurl', 'selected': false}, function(tab){
        console.log("created tab:", tab.id);
    });
    

    UPDATE

    Your get_tab_id() function then should look like this:

    function get_tab_id(url, callback)
    {
        var id = -1;
    
        chrome.tabs.getAllInWindow(null, function(tabs)
        {
            for (var i=0;i<tabs.length;i++)
            {
                if (tabs[i].url == url)
                {
                    id  = tabs[i].id;
                    break;
                }
            }
            callback(id);
        });
    }
    

    Usage:

    var tab_id = -1;
    get_tab_id('http://myurl', function(id){
        console.log(id);
        if(id == -1) {
            chrome.tabs.create({'url': 'http://myurl', 'selected': false}, function(tab){
                console.log("created tab:", tab.id);
                tab_id = tab.id;
    
                restOfCode();
            });
        } else {
            tab_id = id;
            restOfCode();
        }
    });
    
    function restOfCode() {
        //executed after tab is created/found
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To learn JavaScript I am writing a small text editor Google Chrome extension. But
I'm writing a small Chrome extension that would have a content_script . It would
Writing a small HTML web page with some very simple Javascript in it, I
I have a small question concerning css styles and javascript / jquery. Lets say
I'm writing a small Chrome extension to manipulate Google's search results' DOM to add
I'm developing a small html page on Google Chrome, and I' writing it in
I'm writing a small script to open mailto links from webpages in google chrome
I'm writing small php extension and have problem when building it. The code: PHP_RINIT_FUNCTION(pstat)
I have an application where I am reading and writing small blocks of data
Coming from a PHP background, I'm used to writing small functions that return a

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.