I am successully calling a javascript within Vaadin. However how could we pass some parameters to this javascript file.
E.g: below I pass the variable num to the JS file but it does not work.
JAVA:
Div myDiv = new Div();
int num = 1000;
UI.getCurrent().getPage().addJavaScript("src/functparam.js", LoadMode.LAZY);
add(myDiv);
JS file content (functparam.js):
alert('This is a number: ' + num);
Related
I'm trying to implement a variable system for my html web pages. That's why I wrote a function that will take some string, search it for variables, replace them with their corresponding value and the give the new string back:
//Finish HTML files by replacing variables
handlers.insertVariables = function(file, callback){
//Load variables
let variables = require('./variables.js');
console.log(variables) //For debugging only
//Loop through all possible variables and replace
for(let key in variables){
if(variables.hasOwnProperty(key)){
let find = '{' + key + '}';
let replace = variables[key];
file = file.split(find).join(replace)
//file = file.replace('{' + key + '}', variables[key])
}
}
//Callback the new file
callback(false, file);
};
This part works without issues. Its also able to replace multiple instances of the same variable. The problem is now the external variables.js file. I made an external file for these as I will probably a few dozens of these in the future. This is the variable.js file:
//Container with all variables
let variables = {
'landing_videoID': global.newestVideo.id,
'landing_time': new Date(Date.now()).toUTCString()
};
//Export the variables
module.exports = variables;
When the handlers.insertVariables function gets called for the first time, it will receive the up-to-date values. But these then are not changing anymore. Is there something that I'm doing wrong, or is my attempt just bs in general?
Thanks for your help!
Module is cached after first require. One way to solve this problem is to export as function and call it every time as follows:
So, refactor variable.js as follows:
//Container with all variables
function getValues()
return {
'landing_videoID': global.newestVideo.id,
'landing_time': new Date(Date.now()).toUTCString()
};
}
//Export the variables
module.exports = getValues;
Then, require variable.js like this:
//Load variables
let variables = require('./variables.js')();
I have a controller method that returns image from MongoDB, and I want to show it in my view:
<HttpPost()>
Function ShowImage(cardNumber As String) As FileContentResult
Dim Handler = New MongoDBHandler()
Dim newString = cardNumber.Replace(vbLf, "").Trim().Replace("""", String.Empty)
Dim byteArray = Handler.ReadImage(newString)
Return File(byteArray, "image/png")
End Function
I have the javascript function:
function postCardNumber(elm) {
var CardNumber = $(elm).closest("tr").find(".card-number").html();
var $img = $('<img>');
$img.attr('src', "data:image;base64," + #Html.Action("ShowImage", "CreditCard", CardNumber));
$("#myModal").append($img);
}
But there is a red underline under "CardNumber" parameter for the attr function.
Why?
Are you sure that razor templates works with JavaScript? You can translate your razor syntax to js/HTML. But I'm not sure that its works vice-versa. Razor syntax transpiled when your page rendered by the server,js starts working when page loaded. You should rewrite your code to js without using razor in this way
Should be a simple problem but i dont know exactly why its like this.
In my ASP.NET MVC 5 website i have a simple view with a grid, and a cell action that calls a js function sending some parameters to this function.
function OnCellClick(param1, param2) {
var urlAJAX = #Url.Action("GetJson", "PosicaoEstoque", new { p1 = param1 , p2 =param2}); }
So, like this i get the 'Cannot Resolve symbols' for the param1 and param2.
How can i solve it?
You can use placeholder. Generate url using place holder parameters and then replace them with param
function OnCellClick(param1, param2) {
var urlAJAX = '#Url.Action("GetJson", "PosicaoEstoque", new { p1 = -1 , p2 = -2})';
urlAJAX = urlAJAX.replace('-1', param1).replace('-2', param2);
}
Your problem is that you're mixing server-side and client-side code. You cannot simply put JavaScript variables in Url.Action(), as it runs on the server-side. What you can do is to put some dummy values as parameters and call JavaScript's replace() function on generated URL.
Check this for reference.
Without using Url.Action like below:
var urlAJAX = 'PosicaoEstoque/GetJson?p1=' + param1 + '&p2=' + param2;
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 have a flash movie that requires some JavaScript from an include file but I'd like to be able to embed it anywhere without requiring a script tag.
Is there a way to include an external JavaScript file using the ExternalInterface library so I can call the methods in the JavaScript include file later?
Thanks!
Not many people realize it, but you can write inline Javascript in your .as files, and even pass in values, like so:
var someVarInAS : String = 'foo';
var someOtherVarInAS : int = 10;
var jsXML : XML =
<script type="text/javascript">
var someVarInJS = '{someVarInAS}';
var someOtherVarInJS = {someOtherVarInAS};
<![CDATA[
//here be code
alert( 'this comes from flash: ' + someVarInJS + ', ' + someOtherVarInJS );
]]>
</script>;
ExternalInterface.call( "function js_" + ( new Date().getTime() ) + "(){ " + jsXML + " }" );
A few things to note:
the {} inside the javascript code will translate to the value of whatever variable you put in between
the cdata section enables you to write whatever javascript code you want, otherwise the compiler can complain.
All javascript code called through externalinterface should be placed in a named function, otherwise it will not work in a few browsers. In this code snippet I employ a little trick (new Date().getTime()) to ensure the function always has a unique name and can't conflict with another one with possibly the same name.
don't forget the ; behind </script> it tells the compiler your javascript ends there
everything javascript can do, can be done with externallinterface. i think the best way would be to port the js script to ac.
this is how you can include a tag:
ExternalInterface.call("document.getElementsByTagName('head')[0].appendChild(document.createElement('script'))");
ExternalInterface.call("document.getElementsByTagName('script')[document.getElementsByTagName('head')[0].getElementsByTagName('script').length-1].setAttribute('type', 'text/javascript')");
ExternalInterface.call("document.getElementsByTagName('script')[document.getElementsByTagName('head')[0].getElementsByTagName('script').length-1].setAttribute('src', 'http://your.server/your.js')");