Skip to content Skip to sidebar Skip to footer

If Window.location Ends With Html Execute Javascript

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 ishtm

Solution 1:

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

Solution 2:

var winloc = window.location.pathname; //for e.g http://mysite.com/home.htmlvar ishtml = /html$/i.test(winloc); //Remove the i if you want to match only html and not HTMLif(ishtml === true){
    //Your JS
}

Demo

Solution 3:

var winloc = window.location; //for e.g http://mysite.com/home.htmlvar ishtml = winloc.match(/html$/i);
var dothtml = "html";
if(dothtml==ishtml){
//execute javascript here
}

Solution 4:

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...

Solution 5:

var re = /.*(\.html)$/i;
var winloc = window.location.href; //for e.g http://mysite.com/home.htmlvar ishtml = winloc.match(re)[1];
var dothtml = ".html";
if(dothtml==ishtml){
//execute javascript herealert("yes")
}

Post a Comment for "If Window.location Ends With Html Execute Javascript"