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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:41:58+00:00 2026-06-01T00:41:58+00:00

I have a js script which declares a namespace, then has a method called

  • 0

I have a js script which declares a namespace, then has a method called run() which I can call from inside a XUL script like myNamespace.run():

var myNamespace = {
    run: function() {
        var selectedText = getSelText();
        alert (selectedText);

        var getSelText = function() {
            var focusedWindow = document.commandDispatcher.focusedWindow;
            var selText = focusedWindow.getSelection();

            return selText.toString();
        }
    }
}

I want to be able to call getSelText() inside myNamespace.run() without needing to declare getSelText() as another top level function of myNamespace. Instead, it should be like a private method inside myNamespace.run().

When I run this script I receive an error:

getSelText is not a function.

I’m pretty new to JavaScript, so I don’t know the best way of designing this. Is it possible to acheive what I am trying? Am I going about this the wrong way?

Appreciate any help!

  • 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-01T00:41:59+00:00Added an answer on June 1, 2026 at 12:41 am

    If you declare getSelText as a var, it needs to come before any statements where you use it. Just make the var getSelText = ... statement the first statement in the run function.


    Supplemental information:

    var statements in Javascript are “hoisted” to top of the function they’re declared in, so this:

    function() {
        var a = 42;
        // Other stuff...
        var b = 23; 
    }
    

    really means this:

    function() {
        var a = 42, b;
        // Other stuff...
        b = 23;
    }
    

    Another similar construct in Javascript is the function statement, as opposed to function expressions:

    function() {
        var fn1 = function() { return fn2(); }; // Function expression
        alert(fn1());
    
        // Function statement
        function fn2() {
            return "Hello from fn2";
        }
    }
    

    If you ran the code above, it would successfully alert “Hello from fn2”. This is because function statements are hoisted to the tip-top before any other statement within the scope they’re declared in. As you can see, this could get very confusing. Probably the best convention to follow is to use function expressions and declare them in the order that you need.

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

Sidebar

Related Questions

I have the below SQL Script which is run from Excel VBA using ADO
I have a script which will be run interactively by non-technical users. The script
I have a script which, each time is called, gets the first line of
I have a script which collects an image, link and some text content from
I have a python (2.5.4) script which I run in cygwin (in a DOS
I have a code which is: DECLARE @Script VARCHAR(MAX) SELECT @Script = definition FROM
I have a sql script say abc.sql which I am calling from a batch
I have a script which contacts a few sources and tell them the IP-address
I have a script which logs on to a remote server and tries to
I have this script which basically toggles a bgColor class on and off so

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.