0

I am using a series of if statements to manipulate css on a forum using the following:

if(location.href.match(/(showforum=3)/i) != null) {
$(document).ready(function(){
$("#topimg").addClass("announce");
});}

The code works perfectly fine, but every other showforum beginning with a 3 displays this image unless I code it otherwise. So my question would be how do I make my location more exact so that it only makes changes to 3 and not 3x? Is it even possible using this coding?

2 Answers2

1

Change your regex so that the "value's boundary" is checked as well:

var pattern = /showforum=3(?=&|$)/i;
if (pattern.test(location.href)) {
  ...
}

Note the accompanying change in the testing expression: if you only need to find out whether or not some string matches the pattern, you should use regexp.test(string) syntax, not string.match(regexp) !== null.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • I was hoping to squeak by simply with loads of shortcuts, but I suppose I'll have to man up and L2Syntax properly. Thanks. – user2473138 Sep 05 '14 at 04:57
0
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

if( getParameterByName("showforum") == 3 ){
    //........

}

Ref

How can I get query string values in JavaScript?

Community
  • 1
  • 1
Parfait
  • 1,752
  • 1
  • 11
  • 12