如果在某个页面上显示html代码块

时间:2013-05-03 19:51:56

标签: javascript html

我需要获取此人所在的当前页面名称,如果该页面名称与数组中的任何页面匹配,那么我需要显示一个显示搜索框的html块。我怎么能用JavaScript做到这一点?到目前为止,我有这个找到当前页面:

var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

1 个答案:

答案 0 :(得分:0)

var aPages = [ 'page1', 'page2', 'page3' ];

if (aPages.indexOf) {
    if (aPages.indexOf(sPage) >= 0) {
        // We found a match.
    } else {
        // The page is not in the array.
    }
} else {
    // We're using an old browser that doesn't provide support for Array.indexOf.
}