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 10 of 10

Thread: if(url.indexOf

  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    if(url.indexOf

    hi

    i have a code like this to show and hide a div in certain pages.
    I have set the div property in css as display:none'
    so that it will not show on pages other than the search_results.aspx as mentioned in the javascript code below.

    if i need to add one more url to it, how will i do it? a separation with comma helps?

    pls see the code below.

    thanks


    <script>
    var control = document.getElementById("iframe");
    var url = document.URL;
    if(url.indexOf("search_results.aspx") != -1){
    control.style.display="block";
    }
    </script>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    You need to use the || operator (logical OR) and test both patterns separately
    Code:
    if(url.indexOf("search_results.aspx") != -1 || url.indexOf("whatever.aspx") != -1){

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    You can put the urls in an array. Array also has indexOf method for modern browsers. Implement it in browsers that don't support it. I don't know off the top of my head what browsers don't support it. I'm guessing IE8 and below?

    And it's safer to use location.pathname instead of document.URL to avoid matching with querystrings and anchor.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    But in that case you need to check for the full URL and not just parts of it ...

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Why do you need to check for the full URL when you just want to check the filename?

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    I see your point ... but I cannot seem to understand how putting the filenames into an array could help you with finding out whether or not one of them matches the current URL

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Code:
    var urls - ['search_results.aspx', 'default.aspx', 'whatever.aspx' /*, 'and so on...' */],
        url = location.pathname.match(/.*\/(.*)$/)[1];
    if (urls.indexOf(url) != -1){
        //...
    }

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Or even slightly easier, since we know that the file name will end in ".aspx":
    Code:
    var urls - ['search_results', 'default', 'whatever' /*, 'and so on...' */],
    url = location.pathname.match(/(\w+)\.aspx/i)[1];
    if (urls.indexOf(url) != -1){
        //...
    }
    Though most any reasonable pattern match should work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Actually, this is better done in ASP.NET as a server control, no javascript needed.

    Code:
    <iframe id="iframe" runat="server" visible="False"></iframe>
    then in the code-behind, set iframe.Visible = true; accordingly.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Makes ultimate sense to me! Much better done server-side if you have the choice. Good catch.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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