If [removed] ends with html execute javascript

2019-08-17 15:15发布

Execute javascript if current location contains html in in. I have tried the below but it doesn't work

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
}

5条回答
Viruses.
2楼-- · 2019-08-17 15:34
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")
}
查看更多
做个烂人
3楼-- · 2019-08-17 15:40
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
}
查看更多
再贱就再见
4楼-- · 2019-08-17 15:48
var isHTML = "html" === window.location.pathname.split(".").pop().toLowerCase();
if ( isHTML ) {
    //execute javascript here
}

See Amaan answer using regexp:

https://stackoverflow.com/a/8619635/887539

查看更多
贼婆χ
5楼-- · 2019-08-17 15:54
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
}

Demo

查看更多
在下西门庆
6楼-- · 2019-08-17 15:57

My suggestion:

if ( window.location.pathname.match( /html$/ ) ) {
    // do it
}

You don't have to declare local variables if the value are only needed once...

查看更多
登录 后发表回答