Why isn't my try catch error not showing up?
I'm on JSFiddle and for some reason the try-catch error message only shows up when the JS code is within the tags in the HTML tab, but when it's in the separate JS tab, the try-catch error won't print.
This would work:
<!DOCTYPE html>
<html>
<body>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>
<script>
function myFunction() {
var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>
</body>
</html>
But this doesn't:
https://jsfiddle.net/k0rrupt3d/rqejvzoc/
javascript html try-catch
add a comment |
I'm on JSFiddle and for some reason the try-catch error message only shows up when the JS code is within the tags in the HTML tab, but when it's in the separate JS tab, the try-catch error won't print.
This would work:
<!DOCTYPE html>
<html>
<body>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>
<script>
function myFunction() {
var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>
</body>
</html>
But this doesn't:
https://jsfiddle.net/k0rrupt3d/rqejvzoc/
javascript html try-catch
Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.
– Caleb Adams
Jan 2 at 19:37
Your code works fine with codepen: codepen.io/anon/pen/mapjKQ
– Armel
Jan 2 at 19:39
add a comment |
I'm on JSFiddle and for some reason the try-catch error message only shows up when the JS code is within the tags in the HTML tab, but when it's in the separate JS tab, the try-catch error won't print.
This would work:
<!DOCTYPE html>
<html>
<body>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>
<script>
function myFunction() {
var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>
</body>
</html>
But this doesn't:
https://jsfiddle.net/k0rrupt3d/rqejvzoc/
javascript html try-catch
I'm on JSFiddle and for some reason the try-catch error message only shows up when the JS code is within the tags in the HTML tab, but when it's in the separate JS tab, the try-catch error won't print.
This would work:
<!DOCTYPE html>
<html>
<body>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="p01"></p>
<script>
function myFunction() {
var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
if(x == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>
</body>
</html>
But this doesn't:
https://jsfiddle.net/k0rrupt3d/rqejvzoc/
javascript html try-catch
javascript html try-catch
asked Jan 2 at 19:17
k0rruptedk0rrupted
104
104
Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.
– Caleb Adams
Jan 2 at 19:37
Your code works fine with codepen: codepen.io/anon/pen/mapjKQ
– Armel
Jan 2 at 19:39
add a comment |
Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.
– Caleb Adams
Jan 2 at 19:37
Your code works fine with codepen: codepen.io/anon/pen/mapjKQ
– Armel
Jan 2 at 19:39
Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.
– Caleb Adams
Jan 2 at 19:37
Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.
– Caleb Adams
Jan 2 at 19:37
Your code works fine with codepen: codepen.io/anon/pen/mapjKQ
– Armel
Jan 2 at 19:39
Your code works fine with codepen: codepen.io/anon/pen/mapjKQ
– Armel
Jan 2 at 19:39
add a comment |
1 Answer
1
active
oldest
votes
It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() {
to make it global, it will function as expected.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011979%2fwhy-isnt-my-try-catch-error-not-showing-up%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() {
to make it global, it will function as expected.
add a comment |
It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() {
to make it global, it will function as expected.
add a comment |
It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() {
to make it global, it will function as expected.
It appears that the JS section on the fiddle is locally scoped. If you change your function declaration to window.myFunction = function() {
to make it global, it will function as expected.
answered Jan 2 at 19:26
jmcgrizjmcgriz
2,3961410
2,3961410
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011979%2fwhy-isnt-my-try-catch-error-not-showing-up%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your function in the fiddle is defined in a different closure. You can try changing the JavaScript pane settings in your fiddle as suggested here: stackoverflow.com/a/5468413/4925189.
– Caleb Adams
Jan 2 at 19:37
Your code works fine with codepen: codepen.io/anon/pen/mapjKQ
– Armel
Jan 2 at 19:39