0

I wrote this piece of code, but I am not getting my syntax correct. I tried it several ways. The first one below seems like it should work, but I am not quite there yet. Please point me in the correct direction for what the correct syntax is when comparing multiple strings

$(document).ready(function() {
var url = window.location.pathname.substring(1);
if ( [ 'page_id=11', 'page_id=19'].indexOf( url ) > -1 ) { 
$('#main').css("background-color","#ebecee"); 
}
});

and I also tried like this

$(document).ready(function() {
var url = window.location.pathname.substring(1);
if(url.href.indexOf("page_id=11") > -1 && ("page_id=19") > -1 ) {
$('#main').css("background-color","#ebecee");
}
});

What is the correct way to write it?

1
  • There are many "correct" ways to write it. The problem is probably that your URL doesn't contain the value you think it contains. Try console.log(url). Commented Sep 12, 2013 at 0:55

4 Answers 4

1

"A shot in the dark"

Part of the problem may be that you're looking at window.location.pathname and in most cases something like page_id=19 is not part of the pathname -- it is part of the query.

Look at window.location.search instead to check just the query parameters, or, as the others suggested, look at the entire location by checking window.location.href or document.URL.

var query = window.location.search;
if (query.indexOf("page_id=11") > -1 || query.indexOf("page_id=19) > -1) {
    // do your stuff
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks the window.location.search was a great suggestion
I up voted your answer, this is the correct one, for the life of me I couldn't remember window.location.search when I answered.
0

If you're trying to check if either page_id=11 or page_id=19 is in your page's URL, this should work:

$(document).ready(function() {
    if(document.URL.indexOf("page_id=11") > -1 || document.URL.href.indexOf("page_id=19") > -1 ) {
        $('#main').css("background-color","#ebecee");
    }
});

Comments

0
var url = location.href;
if(url.indexOf("page_id=11") > -1 || url.indexOf("page_id=19") > -1 ) {
    $('#main').css("background-color","#ebecee");
}

1 Comment

Thanks this worked I also added window.location.search.substring(1);
0

You can use localeCompare() method.

This method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

Syntax:

string.localeCompare( param )

Return Value:

  • 0 : It string matches 100%.

  • 1 : no match, and the parameter value comes before the string object's value in the locale sort order

  • -1 : no match, and the parameter value comes after the string object's value in the locale sort order

Your case

var index1 = location.href.localeCompare( "page_id=11" );
var index2 = location.href.localeCompare( "page_id=19" );

More

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.