Results 1 to 9 of 9

Thread: using url like a php query string

  1. #1
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using url like a php query string

    hi

    i've written a script which switches between divs of a certain class on the page when appropriate links are clicked.

    for the sake of accessibility, the script first hides all the divs, then shows one of them. this way, if a visitor does not have javascript enabled, the divs will show normally down the page. i've also included named links for anyone without javascript.

    what i want to know is, can i use the #linkname on the end of a url to automaticall switch to the right div when the page loads?

    that way, a bookmarked page would go straight to the right section

    thanks.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Generally with php, it's ?var=value&etc=etc, not #blah

    though that can work too. the # is more for anchors (like... snapping to the bottom of the page). Not sure if there're more uses, though.

    I don't know the script off hand, sorry.

    However, "Is it possible?"... yeah, definitely.

    If you know php alredy, you could have it export some of the javascript... a value of a variable...

    <script>
    var = "<?php echo $_GET['var']; ?>";
    ...
    </script>

    That would be the easy way for php to do it for you.

    However, you may want to use javascript alone... can't be too hard... I just don't have the script at the moment.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Agrajag
    for the sake of accessibility, the script first hides all the divs, then shows one of them. this way, if a visitor does not have javascript enabled, the divs will show normally down the page. i've also included named links for anyone without javascript.
    Sounds good so far.

    what i want to know is, can i use the #linkname on the end of a url to automaticall switch to the right div when the page loads?
    You certainly could parse out the fragment identifier and, if present, use that rather than the default.

    Code:
    function getFragment(uri) {
      var i = uri.indexOf('#');
    
      if (i != -1) return uri.substring(i + 1);
      return null;
    }
    So, for example, you might write something like:

    Code:
    if (document.getElementById) {
      var id, element;
    
      /* If the URI contained a fragment identifier... */
      if (!(id = getFragment(document.URI))
          /* ...and that fragment identified an element within the
           * document, use it to reveal that element.
           */
          || !(element = document.getElementById(id))) {
        /* Otherwise, assume the default. */
        element = document.getElementById('defaultId');
      }
      /* If an element was obtained and it has a style object... */
      if (element && element.style)
        /* ...show that element. */
        element.style.display = '';
    }
    Hope that helps.


    Quote Originally Posted by djr33
    the # is more for anchors (like... snapping to the bottom of the page). Not sure if there're more uses, though.
    The purpose of fragment identifiers in URIs is defined by the URI scheme and the context of its use. In HTML, it provides a means for linking to destination anchors (<a name="..."> or <... id="...">). Its use varies elsewhere.

    Mike

  4. #4
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yeah, thanks for clearing that up, Mike.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi
    sorry i couldn't explain in more detail. i was at school and it was nearly home time, so i didn't have time to upload an example or think about what i was writing .

    so here's an example for you to look at:
    http://zz92.awardspace.com/div%20switcher.html

    basically what i want it to do is, instead of hiding all divs then showing one, when a link is clicked check the url for #n and then show the corresponding div.

    i hope that explains everything.

    also, can anyone tell me why the show_all() function doesn't work? it's meant to show every div like you would see normally without javascript, but in firefox it shows them all for a second then changes back. in ie i does something really weird.

    thanks for all your help.

    agrajag

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If it shows all and switches back, there's something else, perhaps "hide_all" or something, being activated by... something. Maybe just by default.
    Try to see what might be being activated.
    I'd help, but not sure myself. If someone else doesn't figure it out, I'll take a closer look and try to see it, but I'm sure Mike or someone will know
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i've had another look at it, and i'm fairly sure that somehow, clicking the show all link triggers the body's onLoad event. no idea why though, or how to stop or it from happening, can someone please take a look at it?

    thanks

  9. #9
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I hope this fulfills your needs: http://www.anaesthetist.com/mnm/javascript/

    The example is at the bottom of the page (Enter your name here) and to skip the first page and go to directly the tutorial change http://www.anaesthetist.com/mnm/javascript/ to
    http://www.anaesthetist.com/mnm/javascript/part2j.htm#<yourname>

    This captures the text or anything else from a URL after the # character with javascript and displays the result.

Bookmarks

Posting Permissions

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