In my PHP project, there is a javascript code that is supposed to use some values defined in a config file. So, I'm looking for best way to to pass config values to javascript, without mixing php and js. I was thinking that this might be accomplished with Node.js, where config file would be read and parsed, and then this Node module would be converted into browser JS with browserify. Is this even possible ?
If you don't want to mix PHP code with JS code you can run ajax call in javascript to a php function that returns JSON with data you need.
End of function in PHP would be something like:
exit(json_encode($data));
and in javascript (with jquery):
$.post("script.php", {}, function(data) {
var configParams = JSON.parse(data);
});
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.
Is there a way I can read a local JSON file created by the server side into an object in js. So that I can append that data into HTML tags and further create a table of the all that content.
I tried searching a lot and all I found was ajax and jquery which I haven't learnt yet.
From the comments - since you mentioned you already have a file system module on a node.js app that saves the said file, am simply making some assumptions here and giving you an idea on how to read the file. Assuming your file is a json file, using the file system module
var fs = require('fs')
var file = '{yourfolder}/sample.json'
var json = JSON.parse(fs.readFileSync(file, 'UTF-8'))
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
I am new to Javascript and HTML5.
What is the best way to store text data to be read in JS.
I want to load all the characters of a file into an array, the file is similar to a map file and is stored amongst my resource file.
In Java I would create a text file and read each byte into an array, what is the equivalency of doing that in JS? Does JS have a better method of doing this?
Best way - create .js file with JSON encoded data. Almost all languages have functions for this, json_encode in PHP for example.
You can create a file like:
window.data = {something: true, fromfile: ['f','d','s']};
and include it as javascript file on html page:
<script src="data.js"></script>
So you'll have that array in window.data.
Or create a file:
{something: true, fromfile: ['f','d','s']}
And load it with jQuery's AJAX:
$.post('fileURL',{}, function(data) { console.log(data); }, 'json');
and get it at any moment you need it. You can also generate such data at any time with server-side scripts to get actual data, just change fileURL.js to server-side script's URL.
You can also change last parameter (json) to 'text' and get file content as string. But I'd not trust it for binary files, better to convert it on server side to JSON.
I am sending a .XML file using res.sendfile 'xml/foo.xml'. How can I pass variables to the foo.xml file and change the .XML content accordingly?
Thanks
Instead of using res.sendfile 'xml/foo.xml', you'll have to use a template file and pass your variables into a library that will parse the template and give you the XML for each request. While there are many templating engines for Node.js, the two that Express supports by default are EJS and Jade. Jade is Haml-like, which is cool, but if you're working with existing XML, then you should probably stick to EJS. EJS lets you just take ordinary HTML or XML and embed JavaScript in it.
So, you'd rename xml/foo.xml to views/foo.ejs and, instead of res.sendfile, you'd write something like
res.contentType 'text/xml'
res.render 'foo.ejs', obj
where obj contains all of the variables that you want to make available to the template.
Check the Express guide on "View Rendering" for more information.