Results 1 to 7 of 7

This is a discussion on Beginner Javascript Problem within the Plugins Development section, part of the Chrome Plugins category: Hi, I am trying to write a very simple extension that will output the current windowid onto a webpage. I ...


  1. #1
    Peve is offline Junior Member
    Join Date
    Sep 2009
    Posts
    18

    Default Beginner Javascript Problem

    Hi,

    I am trying to write a very simple extension that will output the current windowid onto a webpage. I have the following code:

    Code:
    <script>
    var id;
    
    chrome.windows.getCurrent(function(currentWindow) {
      id = currentWindow.id;
      console.log(id);
    });
    document.write(id);
    </script>
    My problem is that it outputs "undefined" rather than the windowid. I also log the ID to the console and this displays the correct ID.

    I new to javascript so I guess that is where the problem is coming from.

    Any assistance would be great.

    Thanks.

  2. #2
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    The document.write needs to be in the anonymous function you declared.
    Or, remove the var id at the top, to make id a global variable instead.
    It's just a scope issue. When you use "var" you're telling the parser to scope the variable, rather than make it a global one. And even though id exists on the top level scope, it's technically not global. And when using a variable, it uses a variable in the scope - bubbling upwards I think - before using the global of the same name. Pretty sure that's what's happening anyway. ^^;

    If you'd like to still notate the name of the global variable at the top, you can always do something like id = null; or just leave a comment.
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

  3. #3
    Peve is offline Junior Member
    Join Date
    Sep 2009
    Posts
    18

    Default

    Quote Originally Posted by Waha View Post
    The document.write needs to be in the anonymous function you declared.
    Or, remove the var id at the top, to make id a global variable instead.
    It's just a scope issue. When you use "var" you're telling the parser to scope the variable, rather than make it a global one. And even though id exists on the top level scope, it's technically not global. And when using a variable, it uses a variable in the scope - bubbling upwards I think - before using the global of the same name. Pretty sure that's what's happening anyway. ^^;

    If you'd like to still notate the name of the global variable at the top, you can always do something like id = null; or just leave a comment.
    Hi,

    Thank you for the advice. I attempted to place the document.write within the function but it does not seem to work as nothing is printed out at all. It seems very odd as the console.log works fine.

  4. #4
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    What is this code in? A toolstrip? Background page?
    I would expect it to work in a toolstrip, but you can't actually see a background page's document so you wouldn't see any writings.
    If they've allowed API calls in Content Scripts and I hadn't heard that and that's where you're actually doing it from..document.write isn't very useful on a page you didn't document.write the whole thing of.
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

  5. #5
    Peve is offline Junior Member
    Join Date
    Sep 2009
    Posts
    18

    Default

    Quote Originally Posted by Waha View Post
    What is this code in? A toolstrip? Background page?
    I would expect it to work in a toolstrip, but you can't actually see a background page's document so you wouldn't see any writings.
    If they've allowed API calls in Content Scripts and I hadn't heard that and that's where you're actually doing it from..document.write isn't very useful on a page you didn't document.write the whole thing of.
    I am attempting to run it on a new page. So when a user clicks on the toolstrip it opens a new page and then the script runs or at least it should.

  6. #6
    Waha's Avatar
    Waha is offline Senior Member
    Join Date
    Apr 2009
    Location
    Oregon
    Posts
    788

    Default

    So a window.open() from a toolstrip? Or a chrome.tabs.create()?
    And this script is in the new window?
    To be honest though, if you can add an element to the new window it would be much better.
    Say you add a span <span id="output"></span> then you can use
    document.getElementById("output").innerHTML = id;
    ~ Projects ~
    Specialized: Carapass Auction Watcher, Kongregate Chat
    Libraries: bliplib
    Tools: manifest syntax highlighting & snippits
    ~ Happy to make extensions for pay too ;D ~
    Portfolio: Search and Share

  7. #7
    Peve is offline Junior Member
    Join Date
    Sep 2009
    Posts
    18

    Default

    Quote Originally Posted by Waha View Post
    So a window.open() from a toolstrip? Or a chrome.tabs.create()?
    And this script is in the new window?
    To be honest though, if you can add an element to the new window it would be much better.
    Say you add a span <span id="output"></span> then you can use
    document.getElementById("output").innerHTML = id;
    I finally found what the problem was. I was trying to write to the document before the document was fully loaded. Thanks for the help

Similar Threads

  1. Javascript BUG in popup windows
    By bongobongo in forum Bugs and Vulnerabilities
    Replies: 5
    Last Post: 09-06-2010, 07:01 PM
  2. 3D + Javascript + Your web browser
    By Kyrax in forum General Chat
    Replies: 4
    Last Post: 04-23-2009, 02:39 AM
  3. Simple javascript not working - OK on Firefox, IE etc,
    By Skibrowse in forum Bugs and Vulnerabilities
    Replies: 0
    Last Post: 04-17-2009, 10:52 PM
  4. Chrome javascript problem with slilverlight?
    By ashish.tijare in forum Chrome Plugins
    Replies: 0
    Last Post: 03-09-2009, 07:35 AM
  5. javascript form submit
    By Shreesh kaushik in forum Bugs and Vulnerabilities
    Replies: 0
    Last Post: 02-17-2009, 02:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •