how to define dynamic variable in javascript - javascript

when i define in javascript
var whoami = #ViewBag.myname
it is not work or render they render
var whoami = ;
i am trying it #(ViewBag.myname) // not worked too.
are their any way to do this in raor MVC 3

Is #ViewBag.myname empty?
Enclose the variable in quotes, so to have a correct javascript string:
var whoami = "#ViewBag.myname";
also ensure that myname doesn't contain quotes too (or escape them).

Related

Javascript how to pass variable to URL query with window.open

First of all - this question has no answers at
Include variable in URL
or How can I do string interpolation in JavaScript?
I tested many variants from elsewhere before my question here.
For example, as advised at Include variable in URL
window.open('http://example.com/?q="+ myname"');
does not work with the script below.
A kind of specific wrapping is needed.
So, simplest script
<script type="text/javascript">
function sendquery() {
var myname = "John";
alert(myname);
window.open('http://example.com/?q=(myname)');
}
</script>
<button onclick="sendquery()">Query</button>
Alert perfectly shows variable John. But query sends not variable John but (myname).
Or + myname - if follow other answers.
How to wrap variable to URL query ?
It looks like you're just putting the variable in the string incorrectly. Check out template literals if you don't need to support IE.
var myname = "John";
// won't work
window.open('http://example.com/?q="+ myname"');
// will produce 'http://example.com/?q=John'
window.open(`http://example.com/?q=${myname}`);
and likewise
// won't work
window.open('http://example.com/?q=(myname)');
// valid
window.open(`http://example.com/?q=${myname}`);
If you do need to support IE then window.open('http://example.com/?q=' + myname); should work
Your string concatenation is not correct
var myname = "test"
window.open("http://example.com/?q=" + myname); // Classic string concatenation
window.open(`http://example.com/?q=${myname}`); // Using template literal
You have to wrap the URL in Template Literals
window.open(`http://example.com/?q=${myname}`);

Treat String as JavaScript and output variables

Assume I have string '${hello} ${love} times'
I would like to replace hello by the variable named hello and love by the variable named love without removing times. I am using ReactJS with JSX.
My attempt is just removing the $, { and } from the string and then deal with it.
var cut = this.props.string.split(" ");
var one = cut[0].split("{");
var two = one[1].split("}");
var thin = this.var[two[0]];
and then use thin
Your question is completely unclear... Do you know how ES6 template literals work? You have to use backticks to enable string interpolation, not regular quotes ('' or "").
Is this what you are trying to do?
let hello = 'Hello',
love = 'LOVE';
console.log(`${hello} ${love} times`);

Viewdata in jquery syntax

I am working on an asp.net MVC application. I want to access Viewdata and assign it to Javascript variable
var LeaveDays = #(Html.Raw(ViewData["LeaveDays"]));
but it says syntax error at semi colon ; What is syntax. If i remove semi colon, it says statement not terminated. In both cases because of Js error, other scripts are not run on button click or page refresh
I want to assign the value to var
Please suggest.
Pass you value in the quotes.
Use
var LeaveDays = '#(Html.Raw(ViewData["LeaveDays"]))';
try this....
if want to assign to var :
# { var LeaveDays = ViewData["LeaveDays"]; }
or to display :
#Html.Raw(ViewData["LeaveDays"])

adding variable value inside a JSON quote

I am trying to create a javascript object,
var systemName = {"system" : varA};
But I want the object to be in the form `{"system" :"varA"}
with varA having the variable value but inserted inside double quotes.
I have tried {"system" : "'+ varA +'"};
but that did not do the trick. Can you point out what I am doing wrong here? I know its a simple things. But sometimes these small things get us stuck at certain points
Try this instead
var systemName = {};
systemName.system = varA;
(or)
systemName["system"] = varA;
You don't want to do this. You shouldn't do this. If it is a string, the JSON parser will handle it for you. Don't worry about adding quotes to it. There is no reason for you to put quotes around the literal value of the variable. You can put quotes around it at the time of output, if you need to to.
var varA = "Hello";
var systemName = {"system" : varA};
console.log(JSON.stringify(systemName));
// {"system":"Hello"}
http://jsfiddle.net/FWBub/
But, if you must do so:
var varA = '"Hello"';
var systemName = {"system" : varA};
console.log(JSON.stringify(systemName));
{"system":"\"Hello\""}
http://jsfiddle.net/FWBub/1
JSON.stringify(varA) will add JSON quotes around the value.

Replace Word Within Word - Javascript

I need to get a id from a html element and replace a part of the word. For example:
HTML
<input type="checkbox" id="facebookCheckbox"></div>
JavaScript
var x = document.getElementById("facebookCheckbox");
var name = x.id;
name.replace("Checkbox","");
This obviously does not work because the replacing word has to be standalone for it to be replaced. Is there a different way of doing this?
I'm looking for purely javascript no jQuery
Thank you!
name.replace("Checkbox","");
This obviously does not work because the replacing word has to be standalone for it to be replaced.
No, it does work and there's no need to be "standalone" - any part of the string can be matched. Only you did nothing with the result of the operation:
console.log(name.replace("Checkbox",""));
// or
name = name.replace("Checkbox","");
// or assign back to x.id maybe?
You are creating a copy of string when replacing, so you must assign the result of .replace() back to x.id.
var x = document.getElementById("facebookCheckbox");
x.id = x.id.replace("Checkbox","");
this is not going to work in this way. However you can have a marker kind of character by which you can break the name into array and implement the logic. For example:
var x = document.getElementById("facebook_Checkbox");
//Note I have added underscore in the Id
var name = x.id;
var arr=name.split("_");
//Now you have Checkbox and Facebook as string objects (part of array) and you can use them
name=arr[0]
I hope it will solve the purpose.

Categories

Resources