2013-12-11 22 views
1

我試圖替換字符串是這樣的:JavaScript字符串替換錯誤:變量未定義

var thecurrentURL = 'IAmATextString=12345' 
thecurrentURL = thecurrentURL.replace('IAmATextString', '').split('='); 
thecurrentURLvalue = thecurrentURL[1]; 

如果我提醒這個喜歡

alert(thecurrentURLvalue); 

它返回正確的字符串。現在我想檢查字符串是正確的,它在一個div

if (thecurrentURLvalue == '12345') { 
$('#Title').html('12345'); 
} 

寫當我嘗試檢查它不工作,並返回

TypeError: thecurrentURL is undefined  
thecurrentURL = thecurrentURL.replace('IAmATextString', '').split('='); 

問題是什麼?謝謝!

+0

是你缺少 ';'在var thecurrentURL ='IAmATextString = 12345'結尾處; – HICURIN

+0

@hicurin不會有所作爲,這裏一定有其他的錯誤Marc,我沒有看到任何問題 – iConnor

+0

這似乎工作得很好http://jsfiddle.net/mMLU5/ - 你確定你不'你的代碼中有一些額外的範圍? –

回答

0

檢查下面的代碼:

var thecurrentURL = 'IAmATextString=12345' 
thecurrentURL = thecurrentURL.split('=');//Array Contains ['IAmATextString','12345'] 
//thecurrentURL.replace('IAmATextString', '').split('=');//Array Contains ['12345'] Index 0 only available here 
thecurrentURLvalue = thecurrentURL[1]; 

請的jsfiddle here

+0

你的意思是它應該是「thecurrentURL [0];」 ?奇怪的是,當我提醒它時它返回「12345」。但它不起作用,如果我檢查與if條件 –

+0

還檢查與jsfiddle http://jsfiddle.net/Gu9nu/2/ – RGA