2

I'm using jCarousel to load items from a text file dynamically. But I wanted to add an link depending on the image id to the carousel so I'm using regex to work out the image filename and going from there. It all works fine, however I'm getting the following error:

TypeError: Cannot read property '1' of null [http://localhost:64479/TestPage_ToolTips.aspx:71]

Line 70 and 71 are:

 var testRE = url.match("t_(.*).jpg");
        return '<a href="viewprofile.aspx?id=' + testRE[1] + '"><img src="' + url + '" width="75" height="75" alt="" /></a>';

I'm guessing the error is with testRE[1] - however it is working properly in the html when it loads the page. I just don't want a javascript error on the page.

Thanks

1
  • You know what is causing the problem, what are you asking exactly? Commented Nov 12, 2012 at 12:50

1 Answer 1

3

Change your code to this:

var testRE = url.match("t_(.*)\.jpg") || [undefined, ""];

And you should get rid of the error. Mind the escaped dot separator in file name regular expression.

The problem is actually as you anticipate. testRE is null hence you can't access its array elements. This is due to the fact that your string doesn't match. Whether generated URL is correct or not is on your requirements.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually ["", ""]. Else "undefined" will be the id instead of ""
@NULL: Thanks... My bad. I corrected it but a bit differently to arguably consume less memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.