MucaP MucaP - 4 months ago 11
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) {
alert("NaN");
return false
} else if (x > 480) {
alert("Overflow!");
return false
}
window.open("../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

Looking at your example https://jsfiddle.net/9e91gmso/, I think this might be due to scope.

As changing the line function processInp() { to window.processInp = function () { (i.e. https://jsfiddle.net/9e91gmso/1/) 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?

Comments