How to use #using mynamespace in .js file - javascript

I want to put following code in my .js file. It is developed in MVC 5 for using resx file. It is working well if I put this script in view. But is shows error when I put that code in external js file.
<script type="text/javascript">
#using myres = Microsoft.IT.GetDevices.Web.Properties;
$("#divlblNewsSource").text("#myres.Resource.NewsSources");
</script>

You cannot run razor on script files. For this you should use a cshtml file.

Declare a variable and assign the value from the resx file and inside your external JS use this variable for setting the text value.
Inside View
#using myres = Microsoft.IT.GetDevices.Web.Properties;
var temp = "#myres.Resource.NewsSources";
Inside external JS
$("#divlblNewsSource").text(temp);
EDIT:-
You can use RazorJS
It allows C# or VB.NET inside your JavaScript Files. Also contains an http handler for serving, when needed, those files.

Related

ASP.NET MVC - Calling javascript function from another script does not work in view

I am using ASP.NET MVC and I have a javascript file in ~Scripts/bundle/build.js. The file is generated with webpack, which renders a bunch of ES6 javascript files in Scripts. I am loading bundle.js first and then calling the function in a different script like this:
// #Scripts.Render("~/bundles/Webpack")
<script src="~/Scripts/build/bundle.js"></script>
<script>
connectToVnc();
</script>
(first line is commented, I have tried loading the script using bundles in BundleConfig.cs).
I get the error connectToVnc is not defined, even though it is defined in bundle.js.
What am I doing wrong?

How to specify which external JS file to use when calling function

I have to load two javascript files from the same company. These javascript files are different and contain different functions, but they use the same namespace (the company's name). I have both files included in my php file and when I try to create an object specified in one of the external js in my internal js code it searches for the object in the other js file so I get an error saying the function does not exist. When I comment out the other external file include, my code works. How do I force my internal js to look for the function in a specific external js file?
Can you alias the other function to another variable?
e.g:
<script src='functions1.js'></script>
<script>
var function1 = myCompany.someFunction
</script>
<script src='functions2.js'></script>
<script>
var function2 = myCompany.someFunction
</script>

Will an .html file recognize .NET scriptlets or does it have to be convert to an aspx

I believe that I'm trying something that is not possible, but wanted to make sure. I'm trying to set a JavaScript variable inside of an html page. The index.html page to be specific.
I'm doing this:
<script type="text/javascript"> var theLimit = '<%=ConfigurationManager.AppSettings["TheLimit"]%>'</script>
Will it be recognized in an html file, or does it need to be converted to an aspx file?
HTML will not process the server-side command "<%", so you will have to change the page to a format that will process it, such as .aspx (not .ashx).

How to use variables from a php file in a js file?

I have the following file structure:
Now I want to use the variables from the dropdown menu's (that are in the get_attributes.php, get_fields.php and in the get_tables.php) in my main.js and in getData.php . Now the question is how can use a variable from another .php file in a .js file?
You can't directly use PHP variables from JS scripts.
Dynamic access
If you want get variables dynamically you can use AJAX (see jQuery AJAX doc ).
Static access
If you want to access PHP variables when your JS app start you can put PHP variables in the DOM by using this syntax :
<input type="hidden" id="php_name" value="<?=$name?>" />
And use jQuery to get it :
$('#php_name').val();
I hope it will help.

how do i access server data in separate js file

aspx file
string firsrName="jafer";
myscript.js
GetMyName();
function GetMyName() {
alert('<%=firstName%>');
}
I am not getting my value
The line alert('<%=firstName%>'); use the Web Form Page syntax. It is actually not possible to get the value like this because this syntax cannot be used in external JS files.
The simpliest (but not cleanest) method is to write the JS method into the layout file or another aspx file.
Read How to get asp.net client id at external javascript file
You could make a global variable in your aspx page and access it in your js using window.objectName

Categories

Resources