Pass parameter in method - javascript

I have this code
var txt;
function change(){
txt = "#Utils.GeneratePass()";
document.getElementById("password_field").value = txt;
}
function changeToHash(txt){
alert(window.txt);
var password = "#Utils.Hash(txt)";
document.getElementById("password_field").value = password;
}
In method #Utils.Hash i need pass one string, and i want put the value of txt.
But i got the error "The name 'txt' does not exist in the current context.

You cannot pass js variable to C# code,you can try to pass #Utils.GeneratePass() to #Utils.Hash():
var txt;
function change(){
txt = "#Utils.GeneratePass()";
document.getElementById("password_field").value = txt;
}
function changeToHash(txt){
alert(window.txt);
var password = "#Utils.Hash(Utils.GeneratePass())";
document.getElementById("password_field").value = password;
}

Related

How do I use two variables in my function?

So I have multiple script. One script retrieves data from a Googlesheet and parses it as JSON. The other one uses this to output it to HTML.
My first:
function getStatistics() {
var sheet = SpreadsheetApp.openById("ID");
var rowsData = sheet.getRange("A:A").getValues();
var result = JSON.stringify(rowsData);
var funcNumber = 1;
return result;
}
This retrieves the data from a spreadsheet in column A.
The second script, here I want to use both 'Result' and 'Funcnumber' in my function.
function onSuccess(data, funcNumber) {
var dataJson = JSON.parse(data);
var newColumn = document.createElement("div");
newColumn.className = "column";
for(var i = 0; i < dataJson.length; i++) {
if (dataJson[i] != "") {
var div = document.getElementById('cont-' + funcNumber);
var newDiv = document.createElement("div");
newDiv.innerHTML = dataJson[i];
newColumn.appendChild(newDiv);
}
}
div.appendChild(newColumn);
}
Using the Json result to PARSE the HTML works. But retrieving 'funcNumber' from the function not. Then finally I call the first function with this line:
google.script.run.withSuccessHandler(onSuccess).getStatistics();
Does anybody know how to use both result and funcNumber in my second function?
function getStatistics() {
var ss = SpreadsheetApp.openById("ID");
const sheet = ss.getSheetByName('Sheet1');
let result = {data:JSON.stringify(sheet.getRange(1,1,sheet.getLastRow(),1).getValues()),funcNumber:1}
return result;
}
function onSuccess(obj) {
var dataJson = JSON.parse(obj.data).flat();
var newColumn = document.createElement("div");
newColumn.className = "column";
for (var i = 0; i < dataJson.length; i++) {
if (dataJson[i] != "") {
var div = document.getElementById('cont-' + obj.funcNumber);
var newDiv = document.createElement("div");
newDiv.innerHTML = dataJson[i];
newColumn.appendChild(newDiv);
}
}
div.appendChild(newColumn);
}
A single column or row is still a 2d array
Following is the way to make the call in Google script to return the value for the 2nd parameter.
google.script.run
.withSuccessHandler(onSuccess)
.withUserObject(funcNumber)
.getStatistics()
WithUserObject() needs to be called after the withSuccessHandler.
See the documentation below on Google script
withUserObject(object)
Sets an object to pass as a second parameter to the success and failure handlers. This "user object" — not to be confused with the User class — lets the callback functions respond to the context in which the client contacted the server. Because user objects are not sent to the server, they are not subject to the restrictions on parameters and return values for server calls. User objects cannot, however, be objects constructed with the new operator.

JavaScript and jQuery code to return function by var

I am working on small api to get content from my database. I want to show data in a JavaScript/jQuery function according to variables I declared.
example
<script>
function dataset() {
var url = "www.example.com";
var name = "Mini Api";
ver version = "1.00";
this.return url;
this.return name;
this.return version;
}
</script>
How I intend to use it
<script>
minapi = new dataset();
//here I want to return only name
alert(minapi.name);
</script>
Please any idea of how to get this done?
In addition, is there a way to create jQuery function to execute once page load without using this $( document ).ready(function() {});?
I think you want this:
function dataset() {
var myData = {
url: "www.example.com",
name: "mini api",
version: "1.00"
};
return myData;
}
var myVar = dataset();
console.log(myVar.name);
JSFiddle: https://jsfiddle.net/1455d5z3/
This is what you are attempting to do. I fixed ver and assigned properties correctly.
function dataset() {
var url = "www.example.com";
var name = "Mini Api";
var version = "1.00";
this.url = url;
this.name = name;
this.version = version;
}
There might be better ways to do this depending on your situation, but this does give the result you want.
Please use like this.
function dataset() {
var url = "www.example.com";
var name = "Mini Api";
var version = "1.00";
this.returnUrl = function (){
return url;
}
this.returnName = function (){
return name;
}
this.returnVersion = function (){
return version;
}
}
var minapi = new dataset();
alert(minapi.returnName());
Refer Fiddle

trying to create form validation

trying to create form validation to validate so that only alphanumeric characters will be allowed to submit using javascript but nothings working and i dont know what to do next, no jquery just plain js
function validate() {
'use strict';
var errMsg = "";
var result = true;
var fname = document.getElementById('fname').value;
if (!fname.match(/^[a-zA-Z]+$/)) {
errMsg = errMsg + "Alphanumeric Characters only\n";
result = false;
}
if (errMsg !== "") {
alert(errMsg);
}
return result;
}
function init() {
var enquiries = document.getElementById('enquiries').value;
enquiries.onsubmit = validate;
}
window.onload = init;
var enquiries = document.getElementById('enquiries').value;
enquiries.onsubmit = validate;
.onsubmit is being assigned to the value of the enquiries element, rather than the element itself. If you remove .value, your code should work.
plunker: http://plnkr.co/edit/SL7lZzLSje7BEuDRttym?p=preview

Jquery can't pass variable through function

$(document).ready(function() {
//var audit_to_del;
//var type;
//var option_selected;
//var progress;
function redirect(audit_type) {
var page;
switch(audit_type){
case 'Simple 123':
page = 'smeg';
break;
}//end switch
return page;
}
$('#audit_summary_list_div').on("click", ".go_btn", function(e){
var audit_to_del = $(this).prev('.audit_to_del').val();
var type = $(this).closest('tr').find('.audit_type').text();
var option_selected = $(this).closest('td').prev().find('.option_select').val();
var progress = $(this).closest('tr').find('.progress').text();
var location = redirect(type);
alert(location);
});
});
If I pass a literal value through the function it works and returns 'smeg'
var location = redirect('Simple 123');
If I alert(type) the value is correctly shown as Simple 123
If I try to use
var location = redirect(type);
I get an undefined error
I have tried created global variables and then using them in the function
Your text has white-space in it making the condition false. Try that :
var location = redirect($.trim(type));

Pass a variable from razor to javascript and display it

I have a variable
var result = client.DownloadString(query);
in mvc 4 rzaor. By hovering it in the debugging process, it likes the image below
What I want is to display it, so I return it to javascript with the code:
function myFunction() {
$('#response').text('#(Url.Action("result"))');
}
EDIT:
<div id="response"></div>
#{
var strSearch = "test";
var options = "include-passage-references=true";
var client = new WebClient();
var query = string.Format("http://www.xxx.org/v2/rest/passageQuery?key={0}&passage={1}&options={2}", "IP", Server.UrlEncode(strSearch), options);
var result = client.DownloadString(query);
}
However nothing found.
You have to use the ViewBag for that.
On C#:
ViewBag.result = client.DownloadString(query);
On HTML:
function myFunction() {
$('response').text('#ViewBag.result');
}

Categories

Resources