繁体   English   中英

如果网址中有特定单词,如何添加JavaScript?

[英]How to add JavaScript if the URL has a certain word?

我只想将以下jQuery添加到以下页面。

http://www.mywebsite.com/check-8.asp 

http://www.mywebsite.com/edit-8.asp

http://www.mywebsite.com/cart-8.asp

因此,这意味着我想将其添加到URL字符串包含check-8cart-8edit-8

jQuery或JavaScript的最佳方法是什么?

var text = $('#system td.td-main').html();

if (text != null)
{
  var newtext = text.replace("Pris","<div id=\"pricebox\">Pris").replace("mva\)","mva\)</div>");
  $('#system td.td-main').html(newtext);
}

提前致谢。

if(location.pathname.indexOf('check-8') > 0 || location.pathname.indexOf('cart-8') > 0 || location.pathname.indexOf('edit-8') > 0){
//your code here
}

如果您需要纯JavaScript解决方案,请使用window.location属性:

if (window.location.href.match(/(check|cart|edit)-8/).length > 0) {
    // do your stuff
}

您可以使用string.match方法检查它是否与正则表达式匹配。 如果您需要知道它是哪一个,也可以将其排除在外:

var matches = window.location.href.match(/(check|cart|edit)-8/);
if (matches.length > 0) {
    var action = matches[1]; // will be check, cart or edit
}

或者,您可以使用以下命令:

function testForCheckEditCart() {
  var patt = /(check|edit|cart)-8/i;
  return patt.test(location.href);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM