Solved

JavaScript: Prevent TypeError Inline

Posted on 2013-05-22
4
183 Views
Last Modified: 2013-06-03
Is there a way to prevent a JavaScript TypeError inline?  I do not want to have to add any code outside of the alert:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Demo</title>
<script type="text/javascript">


var xyz = 'hello testaspx world';
alert( xyz.match(/[a-z-]+aspx(\b|$)/i)[0] );

var xyz = 'hello world';
alert( xyz.match(/[a-z-]+aspx(\b|$)/i)[0] );

</script>

</head>
<body>
</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 39186709
yes put your code in try/catch block, i.e.

<script type="text/javascript">

try {
var xyz = 'hello testaspx world';
alert( xyz.match(/[a-z-]+aspx(\b|$)/i)[0] );

var xyz = 'hello world';
alert( xyz.match(/[a-z-]+aspx(\b|$)/i)[0] );
} catch(e) {}
</script>

Open in new window


HTH

Ivo Stoykov
0
 
LVL 16

Author Comment

by:hankknight
ID: 39187429
I am looking for a different approach. How can this be done using an inline if, like this?

var c = ((a < b) ? 2 : 3);

Open in new window

0
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 39190005
>
prevent a JavaScript TypeError inline
nevertheless it seems an inline statement, behind the scene there are few actions with results.

the statement xyz.match(/[a-z-]+aspx(\b|$)/i)[0] consists of
1. gets a variable - this returns an object
2. appy a method to the result from precious stem. xyz must be a valid object that has a match method. If it has not you'll get error:
var xyz;
WScript.Echo( xyz.match(/[a-z-]+aspx(\b|$)/i)[0] );
'xyz' is null or not an object

Open in new window

if the method succeed there is next step comming:
3. match returns result - let's say something like
var a =  xyz.match(/[a-z-]+aspx(\b|$)/i);

Open in new window

and here again it tries to apply [0] to this result. It must be an array type. Otherwise and error is rosen.
'xyz.match(...).0' is null or not an object

Open in new window

So to avoid this error the only thing you could do is to surround the code with try/catch like:
try {
  var xyz = 'hello world';
  alert( xyz.match(/[a-z-]+aspx(\b|$)/i)[0] );
} catch(e) { alert( '' ); }

Open in new window

. In either cases you'll receive a result.
HTH

Ivo Stoykov
0
 
LVL 10

Accepted Solution

by:
skij earned 500 total points
ID: 39191342
var xyz = 'hello testaspx world';
alert (( xyz.match(/[a-z-]+aspx(\b|$)/i) ) ? xyz.match(/[a-z-]+aspx(\b|$)/i)[0] : '');

var xyz = 'hello World';
alert (( xyz.match(/[a-z-]+aspx(\b|$)/i) ) ? xyz.match(/[a-z-]+aspx(\b|$)/i)[0] : '');

Open in new window

0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question