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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:54:48+00:00 2026-05-17T20:54:48+00:00

I have declared a local variable named cont in a function named validate. I

  • 0
  1. I have declared a local variable named cont in a function named validate.
  2. I am calling a function process from inside validate.
  3. I am sending the string ‘cont’ as argument to validate function.
  4. In the process function using the string ‘cont’ i want to access the javascript local variable’s value like window[‘cont’]. But i get undefined.
  5. What i am trying to do is trying to access variables like $GLOBALS in php or $$.

Here is an example of what i did.

<script>

function process(str)
{
   alert(window[str]);
}

function validate()
{
   var cont='once there lived a king named midas';
   process('cont')
}

validate();

</script>

The reason is i do most of the forms as ajax. i dont want to make a request string like this.

var param = "command=insert&content=" + encodeURIComponent(cont);

i want to do like this.

var param = makeParam('command,[insert],content,(cont)');

what i do in makeparam is i use regular expression to extract key value pairs.
so i get the string cont from (cont) and i substitute it into window variable like window[cont]. cont will have the string ‘cont’.

so how do we get the content of a variable by using the name of the variable as string?

so i am looking for javascript equivalent of php’s $$

Edited

a part of the code where i extract cont which is inside (cont) which means i want the content of the string between ().

nxt = str[i+1].match(/\((.*)\)$/)

if(nxt)param += '=' + encodeURIComponent(window[nxt[1]]);

the content of param would be

"command=insert&content=once there lived a king"
// assume that once there lived a king is encoded

Edit. Note2.

After few more responses i am editing the code to add this.

I am trying to do like $GLOBALS in php.

I haven’t tried whether $GLOBALS would cantain local variables too.

and learned that local scope will not come into $GLOBALS.


Update after reading Felix King’s Update.

I want to use a function which will construct a query string as simpler as possible. like the following.

var param = makeParam('command,insert,/title/,/keywords/,/description/,mode,[1],fckcontent,(cont)');

// if it is a text without // or () then the it is a straight key value pair. so i will do comment=insert.

//if it is /title/ then the key is title and its value is an input elements value with id as title so title=getElementById('title')

//if it is mode,[1] then mode is the key and 1 is its direct value//

//if it is fckcontent,(cont) then fckcontent is the key and cont is a javascript local variable which will contain html content from a WYSIWYG editor.

// a sample result will be

 var param = "command=insert&keywords=somekeywords&description=somedescription&mode=1&fckcontent=<p>once there lived a king<p>

and then casablanca stated that $GlOBALS will not contain local scope variables and that is the same way in javascript. that’s right.

  • 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-17T20:54:48+00:00Added an answer on May 17, 2026 at 8:54 pm
    function validate()
    {
       var cont='once there lived a king named midas';
       process('cont')
    }
    

    cont is defined in the local scope of the function, not in global scope. Either do just

    cont='once there lived a king named midas';
    

    (without var) or

    window.cont='once there lived a king named midas';
    

    Update:

    But why do you want to go through so much trouble with parsing strings? Why don’t you do:

    var param = makeParam({command: 'insert', content: encodeURIComponent(cont)});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the error : A local variable named 's' cannot be declared in
I have a boolean variable declared at the top of a class and when
The following code fails to compile stating A local variable named 'st' cannot be
In my C# source code I may have declared integers as: int i =
I have a COM object written using the MS ATL library. I have declared
This is a contrived example, but lets say I have declared objects: CustomObj fooObj;
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have the following enum declared: public enum TransactionTypeCode { Shipment = 'S', Receipt
We have our domain model declared in rusty old hbm files, we wish to
I have a c# object with a property called Gender which is declared as

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.