1

I was wondering if there is a way that I can pass variables like this:

<script type="text/javascript">
width = 300;
</script>

<script type="text/javascript">
(function(){
    var k = width;
    alert(k);
});
</script>

I'm trying to learn how I can pass width into the variable k. I dont want to do var width =300 Is there a way to do such a thing like this? so eventually I can place the bottom script, (function(){...}); into a file where I can just do this

<script type="text/javascript">
width = 300;

//more variables if needed

//height = 300;
//name = "example";
//...
</script>

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

So I can add more variables if I have to

Thanks!

5
  • You can do that with the help of cookie Commented Jun 19, 2012 at 4:28
  • what's wrong with your solution? the only difference is that I would only define a single object with all the required variables. and no, cookies are unnecessary. Commented Jun 19, 2012 at 4:29
  • 1
    This should actually work, since the variable is declared before you include the other scripts, it should be available Commented Jun 19, 2012 at 4:30
  • agree with @JonathanOng. it looks like you are trying to create one configuration file. i would put these configuration settings in on object. i would then define variables with select object values. Commented Jun 19, 2012 at 4:31
  • 1
    The code you have works as is - or it would except that the code in the bottom script never gets executed. You need to add a trailing () before the semicolon to invoke your function. Commented Jun 19, 2012 at 4:36

4 Answers 4

6
<script type="text/javascript">
    window.$vars = {
        width: 300
    };
</script>

<script type="text/javascript">
    (function(){
        var k = window.$vars.width;
        alert(k);
    })();
</script>

Putting the variable in global scope will do it.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes You can do this.

If you declare a variable outside of any function or closure that will be a global variable(a property of window object) and you can use that variable anywhere(even in external js files) after that declaration.

But it's better to put all your global vars inside a object so it doesnt pollute the global namespace much like this

globalVars = {
   width: 300,
   height: 200
};

then use them like

(function(){
        var k = globalVars.width;
        alert(k);
    });

Comments

0

it looks like you are trying to create a large configuration setting file. i would create an object and reference it's properties for local variable assignment...

so i would do this:

<script type="text/javascript">
    var myLargeObject = {
          width:300,
          name:"give it a name",
          height:500,
          etc..
    }
</script>

//then reference your values like this...
<script type="text/javascript">
    (function(){
        var k = myLargeObject.width;
        alert(k);
    });
</script>

Comments

0

You could put in a file something like this

function getVariable(width)
{
  var newWidth = width;
  alert(newWidth);
  //do something else
  return newWidth
}

calling the function:

getVariable(300)

or

<a href='javascript:getVariable(300);'>something</a>

is this what you want to accomplish?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.