Если window.location заканчивается html, выполните javascript - PullRequest
0 голосов
/ 23 декабря 2011

Выполнить JavaScript, если текущее местоположение содержит HTML в дюймах. Я пробовал ниже, но это не работает

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 ]

3 голосов
/ 23 декабря 2011
var isHTML = "html" === window.location.pathname.split(".").pop().toLowerCase();
if ( isHTML ) {
    //execute javascript here
}

См. Amaan ответ, используя регулярное выражение:

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

1 голос
/ 23 декабря 2011
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
}

Демо

0 голосов
/ 23 декабря 2011
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")
}
0 голосов
/ 23 декабря 2011

Мое предложение:

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

Вам не нужно объявлять локальные переменные, если значение требуется только один раз ...

0 голосов
/ 23 декабря 2011
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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...