0

現在の場所に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
}
4

5 に答える 5

3
var isHTML = "html" === window.location.pathname.split(".").pop().toLowerCase();
if ( isHTML ) {
    //execute javascript here
}

regexpを使用したAmaanの回答を参照してください:

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

于 2011-12-23T18:49:24.057 に答える
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
}

デモ

于 2011-12-23T18:50:48.690 に答える
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
}
于 2011-12-23T18:43:54.647 に答える
0

私のおすすめ:

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

値が一度だけ必要な場合は、ローカル変数を宣言する必要はありません...

于 2011-12-23T18:52:09.607 に答える
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")
}
于 2011-12-23T18:54:02.457 に答える