MucaP MucaP - 1 year ago 43
Javascript Question

How to open different local files according to the result?

I'm very new to javascript and I'm having an issue with this script:

function processInp() {
var x = parseInt(document.querySelector('input[name="num"]').value);
if (!x) {
return false
} else if (x > 480) {
return false
}"../files/" + x + '.html');
return true

First, it checks if the input is a number, then if it is higher than 480, so throw an alert and an error. That's completly fine and working. What I really can't do is opening the url according to the input. Is it possible with numbers? How? Full example if possible. Thank you!

Stu Stu
Answer Source

Looking at your example, I think this might be due to scope.

As changing the line function processInp() { to window.processInp = function () { (i.e. seems to work correctly?

I think this is a pretty good post about scope if you want to read up further: What is the scope of variables in JavaScript?