i can not use variable as javascript argument - javascript

i am using Javascript on my page.
there is a problem when i use variable to send parameter to function,
when i write complete parameters directly as argument it works good like here
<script type="text/JavaScript">
var X = new MediaController({ContainerDiv:"player",MediaUrl:"test.flv"}');
</script>
but as i use a temp to put this argument in it, and then use temp as argument function it does not work!
<script type="text/JavaScript">
var temp;
temp = '{ContainerDiv:"player",MediaUrl:"test.flv"}';
var X = new MediaController(temp);
</script>
is there a point i missed?

You are assigning a String to the temp variable, which isn't the same as assigning the corresponding object. Instead of this
temp = '{ContainerDiv:"player",MediaUrl:"test.flv"}';
just do this:
temp = {ContainerDiv:"player",MediaUrl:"test.flv"};
and it should work the same.

You are passing in a string, not an object.
var temp = {ContainerDiv: "player", MediaUrl: "test.flv"};
var X = new MediaController(temp);

Related

Oracle Apex changing a String of a object

So I want to get the Object which is essentialy a string. The issue is I cant transfer it into the string format since the resulting string is just anything but the thing I want. Bringing the object into a json doesnt bring a proper string either so my only way of achieving that is the concat method.
I have a Popup-Love which returns the string as follows foo, foo1 ,foo2 while I need it as
'foo1','foo2',...,'foo999' .
My method manages to do that for the first element while all the other elements remove the apostrophe resulting in something like 'foo,foo1,foo2'. How do i fix that?
var i = 0;
if(i == 0){
var t ="'";
var t = t.concat(apex.item("P29_STANDORT").getValue());
var t = t.concat("'");
apex.item("P29_TEST").setValue(t);
i = i +1;
} else {
var t = t.concat("'");
var t = t.concat(apex.item("P29_STANDORT").getValue());
var t = t.concat("'");
apex.item("P29_TEST").setValue(t);
}
You can "overwrite" the native toString() function of the Object and replace it with a function that does what you want. Something like below
function MyObj(){
this.creationTime = new Date().toLocaleString();
}
MyObj.prototype.toString = function something(){
return 'I was created on ' + this.creationTime;
}
var myObj = new MyObj();
console.log('String version of my custom object: ' + myObj);

.slice() is not a function although called on string

I'm trying to figure out some collision, but the compiler keeps showing the error that .slice() is not a function. Here is the code:
var topPos1 = $('#player').css("top");
var rightPos1 = $('#player').css("right");
var topPos2 = $('#player').css("top");
var rightPos2 = $('#player').css("right");
var pos = topPos1.indexOf('px');
topPos1 = parseInt(topPos1.slice(0,pos));
My jQuery is loaded.
Maybe your topPos1 return nothing, if the $('#player') has no css value for top, assigning it to a value will result null or NaN.
You should use log the values to see if the result are correct.
you can't directly slice a number, you can convert it into string then slice after
like this:
topPos1 = parseInt(topPos1.toString().slice(0,pos));
You can just use .replace('px', '') - it will save you one function call

JavaScript dynamic function name

I need to dynamically assign the name of a function to an element of an associative array. This is my attempt which does not work. The problem I am asking for help with is here where I try to call the function: cr['cmd1'](x);
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var cr =[];
var x = 5;
cr['cmd1'] ='foo';
var msg = cr['cmd1'](x);
alert(msg);
function foo(y){
return y;
}
</script>
</head>
<body>
</body>
</html>
Edit: I being passed a string here cr['cmd1'] ='foo'; that I cannot control. That is why I have to work with a string as a starting point from an external application.
Access the functions using this syntax window[function_name]('para1');
Your usage will be something like this
var msg = window[cr['cmd1']](x);
If you want to store it as a function, pass the function directly. Otherwise, if you just want to store it as a string, then you can use the quotes.
Change:
cr['cmd1'] ='foo';
To:
cr['cmd1'] = foo;
I would use window[] and make sure its a function before trying to execute it since you don't have control over what is passed.
var f = window[cr['cmd1']];
if(typeof f==='function') {
f(x);
}
What you are doing there is assigning a function to an array. A more common pattern that you are probably trying to do is to call a function on an object with the array notation.
<script type="text/javascript">
var cr = {};
cr.cmd1 = function foo(y){
return y;
};
var x = 5;
var msg = cr['cmd1'](x);
alert(msg);
</script>
This code results in an alert box that contains the number 5.

Global var in JavaScript

This is annoying me.
I'm setting an array in beginning of the doc:
var idPartner;
var myar = new Array();
myar[0] = "http://example.com/"+idPartner;
And I'm getting a number over the address, which is the id of partner. Great. But I'm trying to set it without success:
$.address.change(function(event) {
idPartner = 3;
alert(idPartner);
}
Ok. The alert is giving me the right number, but isn't setting it.
What's wrong?
Changing the value of the variable does not re-set the values within the array. That is just something javascript can't do automatically. You would have to re-generate the array for it to have the new id. Could you add the id to the value where you use the array instead of pre-setting the values in the array containing the id?
Edit: For example, you would do:
var myArray = [];
var myId = 0;
myArray[0] = "http://foo.com/id/";
and when you need to use a value from the array, you would do this:
var theVal = myArray[0] + myId;
Try this:
var myvar = ["http://site.com/"];
$.address.change(function(event) {
myvar[1] = 3;
}
then use myvar.join () where you need the full url.
The problem here is that at the line
myar[0] = "http://site.com/"+idPartner;
..you perform a string concatenation, meaning you copy the resulting string into the array at index position 0.
Hence, when later setting idPartnerit won't have any effect on the previously copied string. To avoid such effect you can either always construct the string again when the idPartnervariable updates or you create an object and you evaluate it when you need it like...
var MyObject = function(){
this.idPartner = 0; //default value
};
MyObject.prototype.getUrl = function(){
return "http://site.com/" + this.idPartner;
};
In this way you could use it like
var myGlblUrlObj = new MyObject();
$.address.change(function(event){
myGlblUrlObj.idPartner = ... /setting it here
});
at some later point you can then always get the correct url using
myGlblUrlObj.getUrl();
Now obviously it depends on the complexity of your situation. Maybe the suggested array solution might work as well, although I prefer having it encapsulated somewhere in an object for better reusability.
myar[0] = "http://site.com/" + idPartner;
After this line, myar[0] = "http://site.com/undefined" and it has nothing to do with the variable idPartner no more.
So, after that changing the value of idPartner will affect the value of myar[0].
You need to change the value of myar[0] itself.

Assigning variables to objects in JavaScript

I am using the following method to basically create a JSON string.
var saveData = {};
saveData.a = 2;
saveData.c = 1;
However the .a and .c don't cut it for what I need to do, I need to replace these with strings. So something like..
var name = 'wibble';
saveData.name = 2;
This would get accessed with
saveData.wibble
Does anyone know how this could be achieved?
var name = "wibble";
saveData[name] = 2;
alert(saveData.wibble);
Note that, in JavaScript, the following notations are equivalent:
obj.key
obj["key"]
Use the map accessor:
var name = 'wibble'
saveData[name] = 2
You can access Javascript objects using a dictionary notation:
var name = 'wibble';
saveData[name] = 2;
saveData.wibble is now 2.

Categories