Can I execute script from json file - javascript

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.

Related

Convert js file to ulg file

I need to store javascript data into a ulg (Ulog) file. In other words, I need to create a ulg file and convert JavaScript values into ulg and then store it. Can anyone give me some ideas about this?
I had a look into PX4 but it does not support this and I haven't found any post about this either.
FYI: It is also valid the conversion of another type of file other than JavaScript as long as I can convert JavaScript into that type of file. For example: .js -> .ulg or .js -> .py -> .ulg
The end result needs to be an ulg file
I really appreciate the help!
Have a look at this link: https://github.com/PX4/pyulog
Basically, you have to call the ulog2csv script.
For example: in your log’s folder, open a terminal and enter ulog2csv log_name.ulg.

Is there any way to display raw data of a .PHP file with using Java script in my localhost?

I am trying to display the raw contents of a .php file using javascript but i am unable to get it. The php file is in my localhost.
PHP is server code. JS is client code. (Execution talking)
You can't access your PHP files from your .js
The only thing you can do is to get some PHP output using Ajax but I think that is not what you are aiming for.
You can use show_source() function to output the file with the PHP syntax:
<html>
<body>
<?php
show_source("test.php");
?>
</body>
</html>
This function has to parameter show_source(filename,return). filename is required and second parameter is optional if this parameter is set to TRUE, this function will return the highlighted code as a string, instead of printing it out. Default is FALSE
There is no way to use client side code to cause the server to provide the server side source code.
You would need to work on the server to create a URL which provided the data you want.
You could then fetch it using a link, JavaScript's XMLHttpRequest or whatever other method took your fancy.

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

Passing config options from file to javascript

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);
});

element.innerHTML can't call my javascript Function

I am so new to web development and I am encountering this problem. I tried googling this but the problems that I saw is not related to mine.
I have a jsp file that contains only this code:
<script>
displaymessage();
</script>
and in my js file: I have this.
$('ajax_content').innerHTML = resp.responseText;
where in the resp.responseText is equivalent to a string that represents the jsp file.
The function inside the jsp file is existing in the compiled HTML page but the function is not called.
The function's purpose is just to display a message.
Could anybody help me?
Inserting a script via innerHTML does NOT run that script.
Instead, you should remove those script tags and just run the AJAX response through eval.

Categories

Resources