0

I am trying to find array values in URL string? check the condition If requested URL contains array values then pass condition otherwise redirect to google.com

example

     $(document).ready(function () {
        var urls = ["/dept/ce/",
                      "/dept/cs",
                      "/dept/career",
                      "/dept/it",
                      "/dept/mkt/",
                      "/dept/Membership/"
                      , "/dept/pr"
                      , "/dept/PS"
                      , "/dept/ta"
                      , "/dept/wc"
                      , "/dept/PubsProducts"
                      , "_layouts/settings.aspx"
                      ,"/_catalogs/masterpage/"];

       //If document.location.href = "http://localhost/dept/ce/default.aspx" 
       //And Array has "/dept/ce" values
       //Now check document.location.href against array values, if    
      //document.location.href contains array values then pass condition otherwise 
      //else condition 
    });

How to do this?

8
  • This question is very unclear. What are you trying to do? Commented Sep 20, 2011 at 16:50
  • sorry, hard to understand your question. could you rephrase? Commented Sep 20, 2011 at 16:50
  • I don't see exactly what you're trying to do, are you just trying to see if any of the url string values are contained within the current url? Commented Sep 20, 2011 at 16:51
  • Sorry for the confusion. I just rephrased this. Commented Sep 20, 2011 at 16:53
  • 1
    @James123: This really isn't something that should be done with JavaScript. What if I have JavaScript turned off? Commented Sep 20, 2011 at 18:29

1 Answer 1

3

I believe you want to do something, IF the current location URL contains any of the strings in your array. Since you are using jQuery already, I recommend jQuery.grep.

Example:

if ($.grep(urls, function(str) { return location.href.indexOf(str) > -1; }).length > 0) {
  // do one thing
} else {
  // do another thing
}

Link: http://api.jquery.com/jQuery.grep/

Sign up to request clarification or add additional context in comments.

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.