如果当前位置包含html,则执行javascript。我已尝试过以下但不起作用
var winloc = window.location; //for eg https://mysite.com/home.html var ishtml = winloc.match(/html/$); var dothtml = "html"; if(dothtml==ishtml){ //execute javascript here }
var isHTML = "html" === window.location.pathname.split(".").pop().toLowerCase(); if ( isHTML ) { //execute javascript here }
使用regexp查看Amaan的答案:
var winloc = window.location.pathname; //for eg https://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; //for eg https://mysite.com/home.html var ishtml = winloc.match(/html$/i); var dothtml = "html"; if(dothtml==ishtml){ //execute javascript here }
我的建议:
if ( window.location.pathname.match( /html$/ ) ) { // do it }
如果只需要一次值,则不必声明局部变量…
var re = /.*(.html)$/i; var winloc = window.location.href; //for eg https://mysite.com/home.html var ishtml = winloc.match(re)[1]; var dothtml = ".html"; if(dothtml==ishtml){ //execute javascript here alert("yes") }
以上就是jQuery教程分享如果window.location以html执行javascript结束相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/jquerytutorial/557743.html