Solved

JavaScript: Prevent TypeError Inline

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now