开发者

Javascript / jQuery Exec turns up Null

开发者 https://www.devze.com 2023-01-01 00:48 出处:网络
How do I skip over this next line if it turns out to be null? Currently, it (sometimes) \"breaks\" and prevents the script from continuing.开发者_StackOverflow中文版

How do I skip over this next line if it turns out to be null? Currently, it (sometimes) "breaks" and prevents the script from continuing.

开发者_StackOverflow中文版

var title = (/(.*?)</title>/m).exec(response)[1];

$.get(url, function(response){
    var title = (/<title>(.*?)<\/title>/m).exec(response)[1];
    if (title == null || title == undefined){
        return false;
    }
    var words = title.split(' ');
    $.each(words, function(index, value){
        $link.highlight(value + " ");
        $link.highlight(" " + value);
    });
 });        


$.get(url, function(response){
    var title = (/<title>(.*?)<\/title>/m).exec(response);
    if (!title || !title[1]){
        return false;
    }
    title=title[1];
    var words = title.split(' ');
    $.each(words, function(index, value){
        $link.highlight(value + " ");
        $link.highlight(" " + value);
    });
 });

You must check that title is not null before you get the result at index 1

0

精彩评论

暂无评论...
验证码 换一张
取 消