I have following line of code inside my js function saved as separate js file
$('#Page').val(#Html.Raw(Json.Encode(Model.MyFormats));
on page loading inside firebug console I'm getting following error
SyntaxError: illegal character
with pointer to # inside #Html.Raw...
Razor code is not parsed in external files. You need to assign the value of #Html.Raw(Json.Encode(Model.MyFormats) to a javascript variable in the main view and pass it to your external script (for example, assign it to a global variable and then use $('#Page').val(myFormats);)
Related
Can I execute script from json file?
For example:
helloworld.json
{
"script" : "console.log('helloworld');"
}
I want to run the script in the json file in my html or js file.
Point eval to the string to execute it as code
var json = {"script":"console.log('helloworld');"}
eval(json["script"])
Also see Execute JavaScript code stored as a string
Yes, you have to:
Load your json file
Execute the script using eval()
Whether that's a good idea or not though depends on your specific use case.
So I am trying to learn Javascript. I created a JSON file called "Ancest.json". Then, in a new file on netbeans I tried to execute this code accessing that file:
var ancestry = JSON.parse(Ancest);
console.log(ancestry.length);
I am getting a rejection saying "Ancest is not defined".
What am I doing wrong? Attached is a screen shot. Thank you for your time.
JSON.parse method accepts a string which is the JSON object to be parsed into a JavaScript object.
You need to get the content of the file or to move the content of your JSON file into a string variable in the js file. Then you can parse it:
console.log(JSON.parse('{ "a": "test" }'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
You need to assign a string Ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in the full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file.
It also needs correct file system permissions. This is all contingent on who this js is running as, delegate permissions on the parent directories, etc, etc. Chances are permissions are okay for read.
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
I have created a variable named myVar in a js file. The js file is located in the client folder of my meteor app.
I have checked that the js file has already loaded because the console.log('...') has printed but I can not see myVar when I want to call it.
The error message content is :
Uncaught ReferenceError: floaty is not defined
I just run into this problem right now. Try replacing 'var floaty' with 'window.floaty'
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