Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Newbie needs help with Regular Expression​s

    Dear all,

    I have only a very basic knowledge of Javascript, and even less than that with regards to Regular Expressions.

    I've written a simple script in JS designed to "highlight" a navigation bar item when you're on the page it is linking to.

    For example, if you click on "contact us" from the navigation bar, then once the contact page opens, the "contact us" link on the navigation bar will be highlighted.

    My approach is very basic:

    Code:
    var url = window.location.href;
    
    if (url.match("contactus.htm")) {
         ... highlight code...
    }
    This works fine, with one exception: the "default.asp" page!

    If the URL in the address bar is: "http://www.mysite.com/default.asp", then the above method will work just fine.

    However, when someone enters my site directly, the URL in the address bar is "http://www.mysite.com/", with the "default.asp" not being present.

    Now on first thought the solution to this would be to simply modify the script like this:

    Code:
    var url = window.location.href;
    
    if (url.match("default.asp") || url.match("http://www.mysite.com/")) {
         ... highlight code...
    }
    But that won't work since the "http://www.mysite.com/" part is in common to all sub pages on my site.

    Also the default.asp page sometimes takes in query string variables e.g. "http://www.mysite.com/?ref=alex" or "http://www.mysite.com/default.asp?ref=alex".

    Now obviously the solution is to use regular expressions in order to define the specific conditions for the script:

    1. If the url is "http://www.mysite.com/" without anything after the last "/" then highlight the "home page" item in the navigation bar.
    2. If the url is "http://www.mysite.com/?[...]" then also highlight the "home page" item in the navigation bar.
    3. Same goes for "http://www.mysite.com/default.asp?[...]".
    4. Additionally, taking into account that the URL can also begin with "https://secure." instead of "http://www.", with rules 1-3 applying to it as well.

    Unfortunately since I'm a total newbie when it comes to regular expressions, I have no idea where to start with this! I've tried reading some tutorials on Regular Expressions but other than the basic examples I didn't really understand how to do what I want to do.

    For any of you Javascript experts on here this would be extremely easy, and I would REALLY REALLY appreciate your help!!

    Thank you for your time!!

    nanquan

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Why not determine the highlighting either by setting a unique variable value in each page, or by using the value of document.title?

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Logic Ali View Post
    Why not determine the highlighting either by setting a unique variable value in each page, or by using the value of document.title?
    Thanks for the suggestion, I actually thought about that shortly after posting my initial message.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •