?
Solved

JavaScript: Prevent TypeError Inline

Posted on 2013-05-22
4
Medium Priority
?
189 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

719 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