Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript equivalent of php $$ dollar dollar
    text
    copied!<ol> <li>I have declared a local variable named cont in a function named validate.</li> <li>I am calling a function process from inside validate.</li> <li>I am sending the string 'cont' as argument to validate function.</li> <li>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.</li> <li>What i am trying to do is trying to access variables like $GLOBALS in php or $$.</li> </ol> <p>Here is an example of what i did.</p> <pre><code>&lt;script&gt; function process(str) { alert(window[str]); } function validate() { var cont='once there lived a king named midas'; process('cont') } validate(); &lt;/script&gt; </code></pre> <p>The reason is i do most of the forms as ajax. i dont want to make a request string like this. </p> <pre><code>var param = "command=insert&amp;content=" + encodeURIComponent(cont); </code></pre> <p>i want to do like this.</p> <pre><code>var param = makeParam('command,[insert],content,(cont)'); </code></pre> <p>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'.</p> <p>so how do we get the content of a variable by using the name of the variable as string?</p> <p>so i am looking for javascript equivalent of php's $$</p> <p><strong>Edited</strong></p> <p>a part of the code where i extract cont which is inside (cont) which means i want the content of the string between ().</p> <pre><code>nxt = str[i+1].match(/\((.*)\)$/) if(nxt)param += '=' + encodeURIComponent(window[nxt[1]]); </code></pre> <p>the content of param would be </p> <pre><code>"command=insert&amp;content=once there lived a king" // assume that once there lived a king is encoded </code></pre> <hr> <p><strong>Edit. Note2.</strong></p> <p>After few more responses i am editing the code to add this.</p> <p>I am trying to do like $GLOBALS in php.</p> <p>I haven't tried whether $GLOBALS would cantain local variables too.</p> <p>and learned that local scope will not come into $GLOBALS.</p> <hr> <p><strong>Update after reading Felix King's Update.</strong></p> <p>I want to use a function which will construct a query string as simpler as possible. like the following.</p> <pre><code>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&amp;keywords=somekeywords&amp;description=somedescription&amp;mode=1&amp;fckcontent=&lt;p&gt;once there lived a king&lt;p&gt; </code></pre> <p>and then casablanca stated that $GlOBALS will not contain local scope variables and that is the same way in javascript. that's right.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload