如果window.location以html结尾,则执行javascript
如果当前位置中包含html,则执行javascript。我试过下面的方法,但不起作用如果window.location以html结尾,则执行javascript,javascript,jquery,search,window.location,Javascript,Jquery,Search,Window.location,如果当前位置中包含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 } 请参阅使用regexp的答案: 请参阅使用regexp的答案
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
}
请参阅使用regexp的答案:
请参阅使用regexp的答案:
我的建议:
if ( window.location.pathname.match( /html$/ ) ) {
// do it
}
如果只需要一次值,则不必声明局部变量…我的建议:
if ( window.location.pathname.match( /html$/ ) ) {
// do it
}
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")
}
如果只需要一次值,则不必声明局部变量…使用
location.pathname
。您肯定不想匹配:index.php?type=.html
(查询字符串)或foo\hi.html
(哈希)。使用location.pathname
。您肯定不想匹配:index.php?type=.html
(查询字符串)或foo\hi.html
(哈希)。+1。我喜欢你的方法,但在这种情况下,我认为使用正则表达式会更快。如果路径名恰好没有点,这会中断。我只是想做一个JSPerf测试。正则表达式对我来说更快@ŠimeVidas-Disn不中断,只返回整个路径名作为数组中的第一个元素。只有当路径名==“html”+1时,它才会中断。我喜欢你的方法,但在这种情况下,我认为使用正则表达式会更快。如果路径名恰好没有点,这会中断。我只是想做一个JSPerf测试。正则表达式对我来说更快@ŠimeVidas-Disn不中断,只返回整个路径名作为数组中的第一个元素。它将中断的唯一方法是如果路径名==“html”
可能需要添加不区分大小写的/html$/i.test(winloc)
可能需要添加不区分大小写的/html$/i.test(winloc)
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")
}
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")
}