var threadNoCheck = $('#replies tr div a').contains('First post')[0].href; var tN; if(threadNoCheck.indexOf('#') > -1) { if (threadNoCheck.indexOf('&') > -1){ tN = threadNoCheck.slice(threadNoCheck.indexOf('t=')+2, threadNoCheck.indexOf('&')); } else { tN = threadNoCheck.slice(threadNoCheck.indexOf('t=')+2, threadNoCheck.indexOf('#')); } } else{ tN = threadNoCheck.split('t=')[1]; }