Aptana Javascript how to run a single javascript program in aptana - javascript

all I would like to do is run this simple program in Aptana to see if it works, but it doesnt even show up.
I used the Javascript Template from Aptana's selection. here is the program.
<script type = "text/javascript" >
function verse (verseNum) {
var lines = "";
if (verseNum ==1 ) {
lines = "One thing I dont know why...";
} else if (verseNum == 1 {
lines = "All I know time is a valuable thing...";
} else {
lines = "NoLyrics";
}
return (lines);
}
alert (verse(1));
alert (verse(2));
</script>
When I run the program, it shows a blank webpage. I am sure it is a very simple mistake, probably completely wrong format. Any help is appreciated.

You are missing a parenthesis, added here:
} else if (verseNum == 1) {
You need to use your browser's console to discover such errors, although I'm surprised that Aptana doesn't indicate it as well.

Related

What could be causing: "Uncaught TypeError: this.captcha_ip.form.submit is not a function" in this (downloaded) javascript?

I'm a total noob to javascript and also this is my first time doing anything but reading this website so maybe I have gone about this all wrong. I've been using PHP+HTML as a hobby for almost 20 years for basic, static websites, but for an M.Arch university project I decided to build a website for a zine, which includes a contact form where I'd like people to be able to submit articles and attachments.
I downloaded an example form and reskinned it to fit the site, but for some reason, the javascript - which I haven't altered in any way - doesn't seem to work. The source website ( https://html.form.guide/contact-form/contact-form-attachment/ ) doesn't seem to have a contact form for me to ask them what's what, and a bit of googling and looking through other stack overflow questions isn't helping, possibly because I don't know enough about javascript to even have the right keywords.
When I inspect the form in my browser, clicking the submit button brings up this error:
Uncaught TypeError: this.captcha_ip.form.submit is not a function
at FG_CaptchaValidator.OnSuccess (fg_captcha_validator.js:42)
at _OnSuccess (fg_captcha_validator.js:120)
at XMLHttpRequest._StateHandler (fg_captcha_validator.js:143)
Here's the function in the downloaded javascript (I really have absolutely no clue where to even begin to bugtest something like this) that the line is part of:
function FG_CaptchaValidator(captcha_ip,captcha_img)
{
this.captcha_ip = captcha_ip;
this.captcha_img = captcha_img;
this.validatedCode=''
this.validate = function()
{
if(this.validatedCode.length==0 ||
this.validatedCode != this.captcha_ip.value)
{
this.ValidateOnline();
return false;
}
else
{
return true;
}
}
this.OnSuccess = function()
{
var msg = this.GetResponseText();
if(msg == 'success')
{
this.validatedCode = this.captcha_ip.value;
if(this.captcha_ip.form.onsubmit())
{
this.captcha_ip.form.submit();
}
}
else
{
sfm_show_error_msg(msg,this.captcha_ip);
document.error_disp_handler.FinalShowMsg();
}
}
this.ValidateOnline = function()
{
var url = captcha_img.src;
var postStr = this.captcha_ip.name + "=" +
encodeURIComponent( this.captcha_ip.value )+'&fg_validate_captcha=y';
this.Init('POST', url);
this.Send(postStr);
}
}
I hope there's something obvious that's just slightly off in this code, because I really don't know where to start with javascript. I know it's a bit like PHP, lots of similar functionality, just at different ends... but the actual syntax..?
If it's not this code, then perhaps I've messed something up in my reskin of the contact form itself, or the way the scripts are included, or, I don't know.. Any guidance would be appreciated!
EDIT TO BRING STUFF IN FROM COMMENTS/ELABORATE: Within my <head> section for every page I bring in metajava.php: <?php require("metajava.php"); ?> within that file I have the scripts included (amongst others): <script type="text/javascript" src="scripts/gen_validatorv31.js"></script> <script type="text/javascript" src="scripts/fg_captcha_validator.js"></script>
I think these two scripts must be getting included correctly, or the browser's inspection console (I use Brave) wouldn't be pointing me to the proper line of the script in the error, and the scripts wouldn't show up in the sources tab(?)
I don't get any 404 errors; the form simply does not submit or appear to do anything unless I'm inspecting the page and see the console error. If I click submit once (with the inspect console open) I get the error I quoted at the beginning, and if I slam it repeatedly like the migrainous bean I am lately, I get a 500 error.
I've also done a little test of the PHP includes by way of adding
echo "filename-for-each-required-include.php included"; to each php include with the title of the file, and they all come in at the top correctly, though it breaks the captcha so I've removed it again now that I know they're working.
I still wonder if it's a javascript syntax thing that I'm not picking up bc I don't know anything really about javascript. I have modified a few simple scripts before but it was VERY trial and error and this is such a complicated looking expression I don't know where to start - is it even meant to be a function?
PS: Thanks for literally YEARS of solving my problems without my even needing to sign up and ask <3

JavaScript Coding Challenge help - Quiz Question Do While Loop

I'm doing a coding challenge that requests we ask just one multiple choice question - if the answer is correct, it prints a congrats and exits the program. if wrong, it offers the user the chance to try again or exit the program. I've only been coding for a few weeks and it still simpler for me to use if/else if for this - but we need to use a Do While loop. Below is what i have so far and any advice would be appreciated:
let answer = [""]
do {
prompt("What shape is the Earth? \nA: Square\nB: Triangle\nC: Round\nD: Flat");
} while (answer != C);
console.log("I'm sorry you're an idiot.")
let retry = alert("Try again? Y/N")
if (retry == Y) {
//how to reset loop from here?
}
else if (retry == N) {
//how to exit program?
}
if(answer === C) {
console.log("Congratulations! You're not an idiot :)")
}
Let's put your code in a snippet:
let answer = [""]
do {
prompt("What shape is the Earth? \nA: Square\nB: Triangle\nC: Round\nD: Flat");
} while (answer != C);
console.log("I'm sorry you're an idiot.")
let retry = alert("Try again? Y/N")
if (retry == Y) {
//how to reset loop from here?
}
else if (retry == N) {
//how to exit program?
}
if(answer === C) {
console.log("Congratulations! You're not an idiot :)")
}
Now click the Run code snippet button above and watch what happens.
Do you see the error message? That is the first thing to fix, and this is your first step in learning how to debug your code.
Debugging is one of the most valuable skills you will need in programming. For JavaScript in a browser, you will want to get familiar with the Developer Tools that are built into every browser. If you use Chrome or the new Edge browser, here is a guide to the Chrome DevTools.
The DevTools include an interactive Console where you can see error messages and type in expressions to see what they do. They also include a Source view where you can set breakpoints in your source code and look at your variables, and many other debugging features.
A Stack Overflow snippet like the one above has a built-in Console so you can see the error message right here in the page. For code you're running in your own web page, use the browser's developer tools to see errors, test expressions, set breakpoints, and view your variables.

Cannot get ExecuteScriptAsync() to work as expected

I'm trying to set a HTML input to read-only using ExecuteScriptAsync. I can make it work, but it's not an ideal scenario, so I'm wondering if anyone knows why it doesn't work the way I would expect it to.
I'm using Cef3, version 63.
I tried to see if it's a timing issue and doesn't appear to be.
I tried invalidating the view of the browser but that doesn't seem to help.
The code I currently have, which works:
public void SetReadOnly()
{
var script = #"
(function(){
var labelTags = document.getElementsByTagName('label');
var searchingText = 'Notification Initiator';
var found;
for (var i=0; i<labelTags.length; i++)
{
if(labelTags[i].textContent == searchingText)
{
found = labelTags[i]
break;
}
}
if(found)
{
found.innerHTML='Notification Initiator (Automatic)';
var input;
input = found.nextElementSibling;
if(input)
{
input.setAttribute('readonly', 'readonly');
}
}})()
";
_viewer.Browser.ExecuteScriptAsync(script);
_viewer.Browser.ExecuteScriptAsync(script);
}
now, if I remove
found.innerHTML='Notification Initiator (Automatic)';
the input is no longer shown as read-only. The HTML source of the loaded webpage does show it as read-only, but it seems like the frame doesn't get re-rendered once that property is set.
Another issue is that I'm executing the script twice. If I run it only once I don't get the desired result. I'm thinking this could be a problem with V8 Context that is required for the script to run. Apparently running the script will create the context, so that could be the reason why running it twice works.
I have been trying to figure this out for hours, haven't found anything that would explain this weird behaviour. Does anyone have a clue?
Thanks!

Javascript - Controlling Photoshop Externally

I have a basic javascript which works within an Action... However I want to control Photoshop from outside the application (in a bigger script/project).
Basic Code
var numberOfPaths = activeDocument.pathItems;
if (numberOfPaths.length < 1) {
'DO SOMETHING HERE - E.g. copy file to folder'
} else {
'DO SOMETHING ELSE E.g. log and do nothing with it'
}
I am trying to open up an image (one-by-one), and check to see if it has a path, then depending on the If/Else statement, do something with it.
I've searched around and haven't found anything that meets what I am trying to do. I am not familiar with Javascript, more into Python. I've tried to do something like below, targeting photoshop similar to how you would in Applescript. Though I'm at a loss.
...
Photoshop = Application('Adobe Photoshop CC 2014');
var docRef = Photoshop.open ('PATH/TO/FILE');
var numberOfPaths = Photoshop.activeDocument.pathItems;
...
Any help would be appreciated. Cheers!

Is this a bug in Firebug?

I have the following javascript code:
function vocableToTextFieldClickEventHandler(e) {
if (e.keyCode == 13) { //Enter key
if (pausedAfterAnswer) {
pausedAfterAnswer = false;
goToNextVocable();
setAnswerNeutral();
$("#vocableToTextFieldUI").val("");
return;
}
if (textIsOnlyValidCharacters(vocableToTextFieldNode.value) == false){
displayError("Not valid input!");
}
if (answerIsCorrect()) {
displayAnswerCorrect();
getActiveVocable().setPreviousAnswerStatus(1);
pausedAfterAnswer = true;
} else {
repeatList.push("x"); //This line
pausedAfterAnswer = true;
}
}
}
When adding breakpoint to the line commented "This line", it triggers no matter what e.keyCode is. I've gone through this code snippet so many times to find the error, but can't seem to find it. I was just wondering if this might be a fault in Firebug or if it's just a silly programming error. I've had some strange behaviour from Firebug earlier today that didn't make sense either, so I really don't know if I can trust it.
As I can not completely figure this one out, I can atleast conclude that some of the strange behaviour I experienced from Firebug can be fixed by going to the Firebug options and clicking "Reset all Firebug options" and then restarting the browser.

Categories

Resources