I am using C# asp.net mvc3.
In one of my views, say I declare a variable like this:
#{
ViewBag.Title = "title"
string mystr;
}
I need to set the value of the variable mystr in a function of the script. How do I access this variable from the script?
Suppose I have a function like
<Script type="text/javascript">
function(){
var st = "This string is for the global variable"
mystr = st;
}
</script>
mystr will later be used in the html code like this:
<h2>#mystr</h2> .
Is there a similar way of accessing a variable from a function?
mystr in your example is server side variable that lives during cshtml view parsing. So you can't affect it with JS code on the client side. You can try something like this:
<script type="text/javascript">
window.mystr = #(mystr); // Set value while server side processing.
function(){
var st = "This string is for the global variable"
window.mystr = st; // Get/Set value
}
</script>
And use global variable mystr on client side wherever you need.
If you need then set header's text via JS you can do it for example with jQuery:
<h2 id="header"></h2>
JS:
$('#header').text(window.mystr);
Related
I want to create some var in my JADE view in Node.js application, for example:
- var MyVariable = ''
And when i click on the button:
button(onclick="show('#{item._id}')")
I want to change MyVariable to value of item._id
function show(partner_id){
MyVariable = partner_id
}
Unfortunately, the code above won't work
Where are you passing item to the jade template? You should be able to simply assign MyVariable in the same place.
I simply need to refer to a variable from a Java class in my js file, but I am not able to do it.
Here is what I have:
public class MyClass.java{
public final static String JAVA_VARIABLE = "abc";
}
testJsp has script.js included
I need to declare the variable x, from the java file, something like below:
script.js:
function this_is_called(){
//The below is not working
var x = '<%=MyClass.JAVA_VARIABLE %>';
}
Is there some way in which I can refer to the variable declared in MyClass.java from script.js?
In your test.jsp file you can assign your Java variable to a global JavaScript variable.
<script>var JAVA_VARIABLE = '<%=MyClass.JAVA_VARIABLE %>';</script>
Then access it in your script.js file
function this_is_called(){
//The below is not working
var x = JAVA_VARIABLE;
}
You just need to ensure the first step is before the script.js file is loaded.
Check this link where you can find answer to this question.
Passing java value to javascript function
Hope this must be Helpful!
I have this rows of c# code in View razor page:
#{
List<UserContact> userContacts = ViewBag.contacts;
String contacts = Html.Partial("~/Views/Shared/Contacts.cshtml", userContacts).ToHtmlString();
}
I want to use the content of contacts variable in JavaScript function since the contacts is a C# object I cant use this variable in JavaScript function.
Is there any way to use contacts variable in Javascript function?
Maybe since the type is string it can be converted to JavaScript variable?
You can use # directives like you would normally do. You can print it using Html.Raw:
var x = /* this is javascript */
#{
...
#Html.Raw(contacts)
}
Or just call #Html.Partial directly:
var x = /* this is javascript */
#{
...
#Html.Partial(...)
}
Or declare it here:
#{
...
string contacts = #Html.Partial(...)
}
And use it later:
#contacts
Yes there is, you only need to render it inside a script block. Try this:
<script>
var contacts = '#contacts';
alert(contacts);
</script>
I have the following function:
<script>
function assign()
{
var val = "";
val = document.form1.text1.value;
alert(val);
}
I want to access the variable val's value inside jsp tag so that i can pass it to the googlePlus method as a String. I tried making var val as a global variable, but it doesnt work. How can I access the variable val inside the following code?
<%
String output = "";
if ( Boolean.valueOf(request.getParameter("submitted")) == true ) {
Scraping h = new Scraping();
output = h.googlePlus();
}
%>
You can assign the value of the variable using a assignment operator to some hidden JSP field and then use it in the JS using document.getElementById(). the code would be somewhat like:
<input type="hidden" value="<%=output%>">
Or alternatively if your js is residing inside the JSP only
var s = "<%=output%>"; should work!
Cheers!
You can't access javascript variables via JSP Java Code.
Your JSP & Java codes are compiled at server side.
And your javascript runs in a browser.
So send the 'val' variable to your servlet by form submit, ajax or whatever.
I am building a bookmarlet based on this site: http://www.latentmotion.com/how-to-create-a-jquery-bookmarklet/
This is the code of bookmarlet:
javascript:(function(){
var head=document.getElementsByTagName('head')0],
script=document.createElement('script');
script.type='text/javascript';
script.src='http://myserver.com/bookmarlet-remote.js?' + Math.floor(Math.random()*99999);
head.appendChild(script);
})();
void 0
How I can pass a variable from the bookmarlet (above code), to bookmarlet-remote.js ?
I've tried after var myNewvar='myValue', without success, Any Idea?
All JS code on a page (including bookmarklet code and scripts included have) have access to the global scope. If you define a variable without the var prefix it will be available to all other scripts.
It might be a good idea to be explicit about this. do window.myVar = "foo"; to clearly signal that you are working with global variables.
Using var in the function makes it local to that function. To make it global you have to add it to the scope of the window, so:
window.newVariable = window.newVariable || 'Your new value here';
OR
window['newVariable'] = 'Your new value here';
You'd create a public variable.
window.rnd = Math.floor(Math.random()*99999);
In bookmarlet-remote.js you just access the variable.