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
Related
I'm trying spring:message tag to my javascript file, but nothing is displayed. In jsp file and tag spring:message works fine, but if I put js code to js file it's not working.
In js file I use:
password: '<spring:message code="account.enterPassword" />'
Sorry for my bad English.
Spring tags renders on the server side and javascript works in client after server side process is complete. But you can put an <script></script> tag inside your jsp, define your variable on it, and then access your global javascript variable in your external js file:
jsp:
<script>
var password: '<spring:message code="account.enterPassword" />'
//any other variables you want to use
</script>
Now, you can access the password variable inside any other js file.
Note, this script tag must be at top of your js files that you want to variables on it. Also, be careful when picking a name for global variables, because, local variables can override global variables in case of common name.
It is not possible for javascript to have access to a spring tag. spring:message is processed server-side before the page is sent to the client, javascript/jQuery is processed later on the client side.
As a workaround, put the message value in a hidden input on your jsp page. Then get its value in your javascript. In your case:
<c:set var="val"><spring:message code="account.enterPassword"/></c:set>
<input id="enterPasswordId" type="hidden" value="${val}"/>
In your javascript (using jquery) you can then use it as follows:
$('#enterPasswordId').val() //jquery
document.getElementById("enterPasswordId"); //javascript
Is it possible to download a script file using the script tag like this
<script src="http://webserver.com/script.js"></script>
and then be able to read the loaded file content with JavaScript, like this for example:
var scriptContent = window.scripts[0].content;
What i came to notice is that the loaded JavaScript file contents is not accessible by other JavaScript files or am i wrong ?
No, it is not possible to do that.
The source code of a script loaded via src is not exposed via any API made available to JavaScript running in the page.
You could read the value of src and then fetch it using XMLHttpRequest.
The source code of specific functions may be available by calling toString() on them.
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).
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.
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.