Solved

JavaScript: Prevent TypeError Inline

Posted on 2013-05-22
4
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

751 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