How to print out the results of the following snipet of code? - javascript

var ctof = function () {
var celsius = prompt("How many degrees celsius?")
var degC = parseInt(celcius)
var fahrenheit = ((degC * (9 / 5)) + 32)
return fahrenheit
};
document.write(ctof());
The window that appears in my browser (safari) seems to read the line where I prompt the user for input, however it does not write the output of the 'ctof' function onto the browser page. How can I fix this?

The answer to your problem: You mistyped celsius as celcius with two c's.
How to find this kind of mistakes on your own? You can open Chrome DevTools by right clicking on the page and selecting 'inspect'. In Windows you can press F12 and on Mac you can press 'Cmd + Opt + i'.
Now that Chrome DevTools is open, go to the console tab. In here you can see all the errors that are produced by JavaScript code running on that page. When you run your code, fill in the prompt and press enter, you see the following error:
Uncaught ReferenceError: celcius is not defined(…)
p.s: See this page for more awesome shortcuts for the Chrome DevTools.

Related

extension closing after entering prompt input

So I've created a chrome extension where it asks for a prompt to rename something, but for some reason it closes the whole extension after submitting the prompt. It still works, just the extension closes every time a prompt is submitted.
The thing is, for some reason, this doesn't happen to everyone. I tested it on a windows computer and the extension stays open, but on my macbook where I code it, it closes the extension.
I can't find any reason this is happening or any other questions similar to this, any help would be appreciated!
code:
var notename = prompt("Enter name:")
var regPattern = /^[\w\-\s]+$/;
var testthing = regPattern.test(notename)
if (testthing==true && notename.length > 0){
chrome.storage.local.set({["note-"+notenumber+"-title"]: notename}, function(){
})
}else{
alert("Invalid name! Only alphanumeric characters allowed.")
}

Object doesn't support property or method 'ExecWB' when trying to call a print preview dialog in IE9

I am writing a web application for IE9 and I need to open the Print Preview dialog (not the window.print(), but the one where they can set the margins etc.)
I found this code in this question - Open another page or image in print Preview:
function PrintPreview()
{
var OLECMDID = 7;
/* OLECMDID values:
* 6 - print
* 7 - print preview
* 0 - open window
* 4 - Save As
*/
var PROMPT = 1; // 1 PROMPT USER 2 DON'T PROMPT USER
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
window.document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
WebBrowser1.ExecWB(OLECMDID, PROMPT);
WebBrowser1.outerHTML = "";
}
However, this line fails:
WebBrowser1.ExecWB(OLECMDID, PROMPT);
The error message in both IE9 and IE11 is this:
"Object doesn't support property or method 'ExecWB'"
Update: more testing showed that the code works in some IE9/IE11 browsers (such as on my machine) but doesn't work on the browser inside an Azure VM and on my customer's configuration.
WebBrowser1 is not null and has a lot of properties and methods, such as the correct classid, but doesn't have the ExecWB() method.
Turns out that the code doesn't work if this option is set to "Disabled" in IE Tools => Internet Options => Security => Security Settings:

Selenium Webdriver trying to use 'ctrl + t' to open a tab - Javascript

I am running Selenium Webdriver to test a web application that is under development (I have created quite a few tests so far). I have been trying to open a few new tabs in the window opened by Selenium but no success so far. I looked through quite a bit of different solutions but most of them for Java or Python and I'm using Javascript (I need to use Javascript).
Selenium Webdriver: v.3.1.0
OS: Xubuntu 16.04
Browsers: Chrome 55.0.2883.87 and Firefox 50.1.0
I have tried various solutions including:
action sequences, which do nothing in both Chrome and Firefox but complains in Firefox:
driver.actions().keyDown(Key.CONTROL).sendKeys('n').keyUp(Key.CONTROL).perform();
using Key.chord(), which results in no errors, no reaction but it does send the keys - Firefox giving a strange charCode after pressing the buttons
driver.findElement(By.css("body")).sendKeys(Key.chord(Key.CONTROL, 't'));
Key.CONTROL only, which also results in no errors, no reaction but it does send the keys - Firefox giving a strange charCode after pressing the buttons
driver.findElement(By.css("body")).sendKeys(Key.CONTROL + "t");
What I do at the moment is to navigate the driver to a website with javascript keypress detection and see if they were clicked after the 'aaa' :
driver.get("http://unixpapa.com/js/testkey.html");
driver.findElement(By.css("body")).sendKeys("aaa");
driver.findElement(By.css("body")).sendKeys(Key.CONTROL + "t");
This navigates to the page and it it oputputs that on the page detection area:
keydown keyCode=17 which=17 charCode=0
keydown keyCode=84 (T) which=84 (T) charCode=0
keypress keyCode=116 (t) which=116 (t) charCode=116 (t)
keyup keyCode=84 (T) which=84 (T) charCode=0
keyup keyCode=17 which=17 charCode=0
which I believe means that they have been clicked. However, there is no reaction and no tabs created. No error displayed anywhere, no complains. Nothing. I was not sure if this is a bug or a problem or something that I might not be doing right. So if anyone has any idea, please help.
To open new tab you can try to use
driver.executeScript('window.open();');
Try using following (Robot Class):
Robot robo = new Robot();
robo.keyPress(KeyEvent.VK_CONTROL);
robo.keyPress(KeyEvent.VK_T);
robo.keyRelease(KeyEvent.VK_CONTROL);
robo.keyRelease(KeyEvent.VK_T);
protected void openNewTab(String url) {
((JavascriptExecutor) driver).executeScript("window.open('" + url + "','_blank');");
}
use this method to open a new tab with a url

Why isn't this picking up commands issued to the console?

In an attempt to prevent clever visitors from cheating the system on my site, I'm trying to secretly record and send to the server anything typed into the browser's command line. The relevant chunk of code is
window.console.command = function (thiscmd) {
alert('thiscmd = ' + thiscmd); // test
if (SG.consoleCommands) SG.consoleCommands += thiscmd;
}
but the alert isn't working. I'm using Google Chrome, but tried in IE as well.
Relevant fiddle: Go to http://jsfiddle.net/63cL1qoe/ and typed var x = 69; in the console and click enter. Nothing happens.
What am I missing?

Open the Firefox browser console programatically from within an extension using JavaScript?

I have tried attaching toJavaScriptConsole() to a button but this is not working (undefined reference error)
How can I program an XUL button to open the firefox browser console so we can see logs from the extension?
The following snippet will work.
if(HUDService.getBrowserConsole()) // is it already open?
HUDService.getBrowserConsole().chromeWindow.focus();
else
HUDService.toggleBrowserConsole();
If you are in a scope where HUDService is not available then to get access to it do the following:
var devtools = Components.utils.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools;
var HUDService = devtools.require("devtools/webconsole/hudservice");

Categories