views:

52

answers:

3

I want to add the the following jQuery to the following pages only.

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

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

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

So this means I want to add it where URL string contains either check-8, cart-8 or edit-8.

What is the best way with jQuery or 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);
}

Thanks in advance.

+4  A: 
if(location.pathname.indexOf('check-8') > 0 || location.pathname.indexOf('cart-8') > 0 || location.pathname.indexOf('edit-8') > 0){
//your code here
}
Moin Zaman
+1 for using `pathname` instead of hacking around in `href`
bobince
+2  A: 

If you want a pure JavaScript solution, use the window.location property:

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

You can use the string.match method to check if it matches a Regex. You can also factor it out if you need to know which one it is:

var matches = window.location.href.match(/(check|cart|edit)-8/);
if (matches.length > 0) {
    var action = matches[1]; // will be check, cart or edit
}
Lucas Jones
what should I put for /a_regex/ part?
shin
I'm *guessing* what I have now should work, but I've not tested it.
Lucas Jones
Well, I did and it seems to work! :)
Lucas Jones
Thanks. I found out that I don't want to add my jquery if it has cart-8 and check-8. But if it has art-8, then I want to add my jquery. What do you suggest in this case?
shin
check out my code above. just modify the strings in the brackets.
Moin Zaman
@shin: You've already decided on a solution, but you'd just remove the 'c' from cart. Regexes can be a powerful tool - I only know the basics, but they make lots of string-handling much easier.
Lucas Jones
+2  A: 

Or you can use the following:

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