Solved

JavaScript: Prevent TypeError Inline

Posted on 2013-05-22
4
188 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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

628 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