如果window.location以html结尾则执行javascript

发布于 2024-12-22 19:35:12 字数 252 浏览 4 评论 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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾挽 2024-12-29 19:35:12
var isHTML = "html" === window.location.pathname.split(".").pop().toLowerCase();
if ( isHTML ) {
    //execute javascript here
}

使用正则表达式查看 Amaan 答案:

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

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

老旧海报 2024-12-29 19:35:12
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
}

演示

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

深海夜未眠 2024-12-29 19:35:12
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
}
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
}
街角卖回忆 2024-12-29 19:35:12

我的建议:

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

如果该值只需要一次,则不必声明局部变量......

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

萌面超妹 2024-12-29 19:35:12
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")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文