I try to use a JSON object (all the object not just a piece) from Node through Jade inside my local myScript.js script.
And my Jade file:
span(class="glyphicon glyphicon-pencil" onclick="confirm(\"#{myJSON.taskid}\", \"update\", !{myJSON})")
But I have this:
Uncaught ReferenceError: myJSON is not defined.
Then I quit !{myJSON} as parameter in the call of function confirm() in onclick(), and I try with a script block:
script(type= "text/javascript" src="myScript.js").
var local_json =!{JSON.stringify(myJSON)};
In myScript.js I issued console.log(local_json), but I end up with the following error in the console:
Uncaught ReferenceError: local_json is not defined.
How I can do it?
You have to first create variable , then import your script file. so basically var local_json = !{JSON.stringify(myJSON)}
then import other script - script ( type = "text/javascript" src="myScript.js")
Related
I'm working with a total of 3 files.
index.html:
...
subWindow
...
The fetch() function returns a callback in JSON format.
subWindows.js:
let micWindow
function subWindow(data) {
micWindow = new BrowserWindow({
width: 1280,
height: 800
})
console.log(data)
micWindow.show()
micWindow.loadFile('src/subWindow.html')
micWindow.webContents.on('did-finish-load', function() {
let javascript = `document.querySelector('body').innerHTML = '` + data + `'`
micWindow.webContents.executeJavaScript(javascript)
})
}
And finally subWindow.html which contains pure html. It also imports subWindows.js:
...
<!-- JS Imports -->
<script src="js/subWindows.js"></script>
...
Firstly, index.html console log prints JSON data for both onclick and subWindow() at MIC.js?[sm]:10. The subWindow.html shows [object Object] from executeJavascript, but the console is empty.
Secondly, doing the following doesn't work:
let javascript = `document.querySelector('body').innerHTML = '` + JSON.stringify(data) + `'`
or
let javascript = 'console.log(' + data + ')'
Both throw an exception Uncaught SyntaxError: Unexpected identifier in subWindow.html
JSON data isn't rendered by subWindows.js in subWindow.html but rather in index.html:
index.html imports <script src="js/subWindows.js"></script>
Why is it throwing an error?
How to pass JSON data from main to remote window as function parameter?
Preferably pass JSON to executeJavascript and inject it back into remote window's html.
Thanks for any feedback!
I'm not sure about the errors, but my solution is to put the data into the window by editing the HTML. Here is one way to do this:
// win is the window you are passing info to
var data = win.document.createElement('span');
data.innerHTML = json;
win.document.body.appendChild(data);
The program in the window would wait for the element to appear, and then use the value for the function.
Note:
I just noticed that your JavaScript doesn't have any semicolons at the end of the lines. You need to have one after every statement. :)
I know some JS and I have a fair amount of experience with VBA scripts for MSO, but I'm just starting to learn how to script google docs, and I'm having difficulty with the google scripting environment.
I created a doc on my google drive, and I am trying to log outputs in the code, but I get an error when I get the error TypeError: Cannot find function getBody in object Excel Download Macro Log File. (line 56, file "Code") when I try to run this code:
function logDataFromCode() {
var LogDoc = DriveApp.getFilesByName('Excel Download Macro Log File').next();
// Access the body of LogDoc, then add a paragraph with relevant data from script:
LogDoc.getBody()
.appendPageBreak()
.appendParagraph("[put variables to log here]");
}
Can someone please explain why I am getting this error?
You are getting that error because the class returned by getFilesByName("name").next() is of type File, not Document.
The solution is to use the File from DriveApp to explicitly open a Document via DocumentApp:
var LogDoc, files = DriveApp.getFilesByName("the file name");
if(files.hasNext()) {
LogDoc = DocumentApp.openById(files.next().getId());
} else {
// No files matched the name supplied to DriveApp
return;
}
Your variable LogDoc is a File, but you want it to be a Document
The best way to cast it is to get it's id and then accesing it using the DocumentApp to open it.
Like this:
var LogFile = DriveApp.getFilesByName('Excel Download Macro Log File').next();
var LogDoc = DocumentApp.openById(LogFile.getId());
// Access the body of LogDoc, then add a paragraph with relevant data from script:
LogDoc.getBody()
I've set up acceptance tests with codeception and phantomjs.
My website contains script elements:
<script src="/bla/test.js" type="text/javascript"></script>
The JavaScript file looks like this:
Bla = {
request: function(var) {
}
};
It looks like the content cannot be found since one of the buttons contains
onclick="return Bla.request('100')"
Running the acceptance test which clicks on this button shows me:
[Selenium browser Logs]
17:10:24.876 WARNING - SyntaxError: Parse error
17:10:25.121 WARNING - ReferenceError: Can't find variable: Bla
onclick (http://localhost/bla)
Screenshot and page source were saved into '/var/www/bla/_output/' dir FAIL
Does the first parse error already mean that the files have been loaded but contain errors? If so, how can I see the exact parse errors?
Requesting the JS file manually in the acceptance test works and it is the correct file (as the output shows):
$I->amOnPage('/bla/test.js');
I run codeception with the option --debug and looked at the output files where everything looked correct. How can I see if the JavaScript file has been imported correctly and if the parse error means that it failed, how do I get more information about the error?
edit:
Even if the parse error is not present I cannot access variables from a different JavaScript file.
When I have multiple script elements:
<script src="/bla/test0.js" type="text/javascript"></script>
<script src="/bla/test1.js" type="text/javascript"></script>
I get an error:
ReferenceError: Can't find variable: Bla
in file test1.js if it was defined in test0.js. This works in the normal browser without errors.
I have 2 files, one named client.php and one named habbo3.js. Inside the client.php there is something called "connection.info.host": that usually needs to have an IP.
However I'm trying to use it as a variable and get the IP from the JavaScript file. I tried doing it like down below but I'm getting an error on chrome developer tool
client:21 Uncaught ReferenceError: price1 is not defined(anonymous function) # client:21
My client.php file: http://pasted.co/0a5a803e
My habbo3.js file:
var price1 = 123.456.789;
var price2 = 222.222.222;
The numbers above are just false numbers but should be replaced with my VPS IP address. Any solution for this guys?
You're trying to load habbo3.js with this line:
<link src="http://.../habbo3.js">
You should replace it with:
<script type="text/javascript" src="http://.../habbo3.js"></script>
I'm trying to execute a java program from within an HTML file using Javascript (specifically, the assignment is to activate an FTP server from an HTML interface, so I need to execute the FTPServer class from a function on my HTML page). I've been trying to use the following code to execute the "program" class which should simply print out "hello." The Chrome console says "Uncaught ReferenceError: java is not defined"
var p1 = java.lang.Runtime.getRuntime().exec("javac program.java");
document.getElementById("test").innerHTML = "working";
var p2 = java.lang.Runtime.getRuntime().exec("cmd /c java program");
var stdInput = new java.io.BufferedReader(new java.io.InputStreamReader(p2.getInputStream()));
var stdError = new java.io.BufferedReader(new java.io.InputStreamReader(p2.getErrorStream()));
I've also tried doing Packages.java.lang.Runtime... to which I get the error "Packages is not defined." I've tried importing the java.lang package:
importPackage(java.lang);
but I get the same error, "Uncaught ReferenceError: importPackage is not defined"
Why isn't importPackage working, and how can I get Runtime to work?
What you are trying to do only makes sense in rhino.
According to Rhino Migration Guide you can try something like this:
var Runtime = Java.type("java.lang.Runtime");
and then run it with
Runtime.getRuntime().exec("javac program.java");