1
A
回答
4
if (location.pathname=="/"){
// at the root of the site
}
更多關於(跨瀏覽器)window.location
對象的信息。
編輯:如果你的 '根' 由名爲index
某些文件可能擔任(如index.html
,index.php
,index.asp
等),你可能想:
var rootPattern = /^\/(?:index\.[^.\/]+)?$/i;
if (rootPattern.test(location.pathname)){
// …
}
或更明確:
switch(location.pathname){
case '/':
case '/index.html':
case '/index.php':
case '/index.asp':
// …
}
0
if (location.pathname == "/" || location.pathname == "/index.html" || location.pathname == "/index.php"){
// at the "main page" of the site
}
+1
或更簡潔:'switch(location.pathname){case'/':case'/index.html':case'/index.php':...}' – Phrogz
相關問題
- 1. PHP中的當前頁面網址
- 2. jquery檢查所選頁面是否爲當前頁面
- 3. 如何區分當前頁面是否爲EPiServer頁面
- 4. 如何訪問當前[網頁]頁面(當前頁面之外)?
- 5. 檢查當前頁面是否爲Jekyll上的主頁
- 6. Chrome是否認爲當前頁面是安全的?
- 7. 挑選網址之前的頁面
- 8. 如何檢查當前頁面是否是Ionic2的主頁?
- 9. 在prestashop中獲取當前頁面和網址
- 10. Html Amp Post顯示當前頁面的網址?
- 11. 如何獲取GAE模板中當前頁面的網址
- 12. 獲取頁面的當前網址(使用URL Rewrite)
- 13. Nightwatch將頁面的當前網址分配給varriable
- 14. 如何將當前頁面網址與導航欄相匹配?
- 15. vBulletin檢查當前頁面是否爲usercp.php?
- 16. MediaWiki自定義皮膚:檢查當前頁面是否爲主頁面
- 17. 確定當前的Drupal頁面是否爲搜索結果頁面?
- 18. 基於虛榮網址獲取Google+商家頁面的網址
- 19. Facebook - 此網址是否存在網頁?
- 20. WordPress的:檢查當前頁面之前或之後是否有兄弟頁面?
- 21. 獲取當前頁面網址並將域名替換爲域名
- 22. 是否可以使網頁網址與iFrame網址相同?
- 23. 當前頁面前頁面的標識
- 24. Windows 10開發:如何檢查當前頁面是否是特定頁面?
- 25. 描述性網址與基本網址
- 26. 基本網址和基本鏈接網址有什麼區別?
- 27. 是否可以僅使用CSS選擇基於當前頁面的元素?
- 28. WebView:如何找到當前頁面的基本URL?
- 29. CakePHP,是否有可能在網址前面使用記錄ID
- 30. 檢查當前頁面是否是索引中的索引
請注意,儘管在JS中可以這樣做,但如果要阻止用戶運行JS的某些部分,則不一定是個好主意。 JS運行在客戶端上,因此用戶可以完全控制它並可以根據需要進行更改。 – nico