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 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Previous Page Variable

    I need a variable to be available on each page of my website that indicates which page the user has just come from. So if the user in now on the "contact" page, and came there from "home", the variable should read "home", or some number corresponding to "home". How would I do this with JS?

    I thought about having each site page declare a JS variable, and then pass it to the next page. How would I code this variable passage between pages?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Set a cookie.

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    80
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Set a cookie.
    No good if user has cookies disabled.


    This script displays a breadcrumb trail. Useless though if javascript is turned off.

    Code:
    function breadcrumbs(){
      sURL = new String;
      bits = new Object;
      var x = 0;
      var stop = 0;
      var output = "<a href=\"/\">Home</a>  >  ";
      sURL = location.href;
      sURL = sURL.slice(8,sURL.length);
      chunkStart = sURL.indexOf("/");
      sURL = sURL.slice(chunkStart+1,sURL.length)
      while(!stop){
        chunkStart = sURL.indexOf("/");
        if (chunkStart != -1){
          bits[x] = sURL.slice(0,chunkStart)
          sURL = sURL.slice(chunkStart+1,sURL.length);
        }else{
          stop = 1;
        }
        x++;
      }
      for(var i in bits){
        output += "<a href=\"";
        for(y=1;y<x-i;y++){
          output += "../";
        }
        output += bits[i] + "/\">" + bits[i] + "</a>  >  ";
      }
      document.write(output + document.title);
    }

    Cheers
    Joe
    http://www.Kanotix.com --My OS of choice, the best hardware detection on the planet!-
    http://distrowatch.com -Linux is the future-
    http://www.packetstormsecurity.org -Security Gurus-

  • #4
    Regular Coder Actinia's Avatar
    Join Date
    Feb 2007
    Location
    Essex, UK
    Posts
    250
    Thanks
    1
    Thanked 23 Times in 22 Posts
    You could do this via the link to each page.

    If you set a variable within each page identifying it, such as:

    var pagename = "home";

    Then in your links, you can append this to the linked filename:

    <a href="contacts.htm?"+pagename >

    Ideally, you would do this by manipulating the DOM by modifying the atrtribute "href" for all <a> tags.

    Within each page, you can then extract the Query String:

    var source = location.search;

    This will set 'source' to "?home" so you will need to remove the question mark.

    John


  •  

    Posting Permissions

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