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!
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!
Share Improve this question asked Jun 19, 2012 at 4:26 hellomellohellomello 8,59742 gold badges153 silver badges310 bronze badges 5- You can do that with the help of cookie – coolguy 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. – Jonathan Ong 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 – woutr_be 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. – chrisvillanueva 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. – gilly3 Commented Jun 19, 2012 at 4:36
4 Answers
Reset to default 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.
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);
});
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>
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 acplish?