我有一個畫廊網站中的每一個畫廊圖像通過像這樣一個url表示:使用javascript檢查url內的數字?
http://www.example.com/gallery/my-photos#10
http://www.example.com/gallery/my-photos#11
http://www.example.com/gallery/my-photos#12
。 。 。 。 。
我創建了一個條件語句來防止用戶無效的url。
var galleryNum = window.location.hash.substring(1);
if(typeof(galleryNum) == "string" || typeof(galleryNum) == "NaN"){
console.log('this is not a number');
}else if(typeof(galleryNum) == "number"){
console.log('this is a number');
}
然而,這並不工作,我從window.location.hash.substring(1)將永遠是一個字符串,無論我輸入數字或字符串獲得的價值。
你應該添加的基數參數:'parseInt函數(galleryNum,10)' –
是的,你definitley應該使用基數參數它在舊版本的ecmascript中做了一些不需要的事情。 – John
@RobM。是的,謝謝你的提醒。 – Yangguang