Reputation: 2669
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
Upvotes: 2
Views: 110
Reputation: 105019
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.
Upvotes: 3