如果当前位置包含html,则执行javascript。我已尝试过以下但不起作用
var winloc = window.location; //for e.g http://mysite.com/home.html
var ishtml = winloc.match(/html/$);
var dothtml = "html";
if(dothtml==ishtml){
//execute javascript here
}
答案 0 :(得分:3)
var isHTML = "html" === window.location.pathname.split(".").pop().toLowerCase();
if ( isHTML ) {
//execute javascript here
}
使用正则表达式查看Amaan答案:
答案 1 :(得分:1)
var winloc = window.location.pathname; //for e.g http://mysite.com/home.html
var ishtml = /html$/i.test(winloc); //Remove the i if you want to match only html and not HTML
if(ishtml === true){
//Your JS
}
答案 2 :(得分:0)
var winloc = window.location; //for e.g http://mysite.com/home.html
var ishtml = winloc.match(/html$/i);
var dothtml = "html";
if(dothtml==ishtml){
//execute javascript here
}
答案 3 :(得分:0)
我的建议:
if ( window.location.pathname.match( /html$/ ) ) {
// do it
}
如果只需要一次值,则不必声明局部变量...
答案 4 :(得分:0)
var re = /.*(\.html)$/i;
var winloc = window.location.href; //for e.g http://mysite.com/home.html
var ishtml = winloc.match(re)[1];
var dothtml = ".html";
if(dothtml==ishtml){
//execute javascript here
alert("yes")
}