jquery - How can I pass variables to external javascript file that can be accessed by all the functions -


i have kendo grid events property hooked function(grid_onrowselect) in external javascript file. there other functions in external javascript file ( on button click * $("#btns").on('click', function () {....* ) , few others. grid_onrowselect function , other functions use common set of variables. how can pass variables external javascript file view (cshtml) can accessed functions.

@(html.kendo().grid<mymodel>() .name("rgrid") .events(events => events.change("grid_onrowselect")) .columns(columns => {     columns.command(command =>   .......  .......  ....... 

the external js file is

var myfunc = myfunc || (function () { var _args = {}; // private  return {     init: function (args) {         _args = args;         // other initialising     },     helloworld: function () {         alert('hello world! -' + _args[0]);     },     grid_onrowselect: function (e) {          var data = this.dataitem(this.select());         detailrequestid = data.id;          var url = _args[1] + "/" + detailrequestid;         window.location.href = url;     },     onerror: function (e, status) {         //alert("a server error has occurred!");         var url = _args[2];         window.location.href = url;      } }; 

}());

how i'm trying pass arguments

  <script>     window.onload = function(){     var searchurl = @url.action("search");     var updateurl = @url.action("update");     var errorurl = @url.action("servererror", "error"); }; myfunc.init([searchurl, updateurl, errorurl]);</script><script src="~/scripts/index.js"></script> 

but when grid_onrowselect or of functions gets executed _args undefined. not correct?

thanks.

if declare variable outside of function in javascript available code js code loaded after it. fact file external not matter, loads dom after place set global variables.

example

var globalvar = "i global"; function test(){     var copy = globalvar;//copy == "i global"     var nonglobalvar = "i not";//this local function } var global2 = globalvar;//global2 == "i global" var anothervar = nonglobalvar;//this line throw error because variable out of scope. 

another common tactic write values hidden field in html , access external functions.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -