SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using document.referrer

    hi,

    we have an site with 3 different versions...dev, editorial and master. I want to create a link to a
    script that we can store in the bookmark toolbar which can be clicked on to shift between the 3 different
    versions of the site....so if the user is on dev looking at x.html i want the script to be able to let the user
    to shift and view the same x.html in editorial and dev just by clicking the link in the favourites toolbar.

    So i made a start...i created html file, called next.html here, that simply alerts out the referrer (i known it's not 100% reliable but it's the nest i can do with
    )....this works fine...when the link is activated from within a webpage:

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
     
    <body>
    <a href="next.html">
    next page
    </a>
    </body>
    </html>


    and then in next.html i have this:

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    alert(document.referrer);
    </script>
    </head>
     
    <body>
     
    </body>
    </html>

    so if one clicks the link saying 'next page' you get an alert with the correct referrer in it.
    However if you make a bookmark of next.html, go to another page, and then click on it the alert comes back with nothing.

    Any ideas how i could do this?

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its ok i've got it....i'm just going to have the bookmark reload the same page using document.location and append a query string on the end.
    If the query string is there then have a script that picks this up looks at the url....looks at the host address and then replaces the host address with the next host address and redirect to this.

    So if i'm on dev and someone clicks the bookmark...it'll use document.location+ querystring=1 to redirect (the same page basically) then my script will see that
    the user is on dev for instance (using index of) then i'll just use replace to get rid of the current host and replace it with the next host in line.

    If that makes any sense to anyone!

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    You can use the following, which can be turned into a bookmarklet in the toolbar

    Code javascript:
    function getDomainName(url) {
    	if (!url) {
    		return null;
    	}
    	return url.match(/:\/\/(.+)\//)[1];
    }
    function changeDomainName(domain) {
    	location.href = location.href.replace(/:\/\/(.+)\//, '://' + domain + '/');
    }
    var availableDomains = [
    	'here.site.com',
    	'there.site.com',
    	'elsewhere.site.com'
    ];
    var usedDomains = [];
    var i, j, domainFound;
    usedDomains.push(getDomainName(document.referrer));
    usedDomains.push(getDomainName(location.hostname));
    for (i = 0; i < availableDomains.length; i += 1) {
    	domain = availableDomains[i];
    	domainFound = false;
    	for (j = 0; j < usedDomains.length; j += 1) {
    		if (usedDomains[j] === availableDomains[i]) {
    			domainFound = true;
    		}
    	}
    	if (!domainFound) {
    		changeDomainName(availableDomains[i]);
    		break;
    	}
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    Thanks for that....interestingly I found a similar way to do it....it's not as bullet-proof as your example but seems to do the trick.

    I just created a page and put this link in it:

    Code JavaScript:
    <a href="javascript: var loc = location.href; 
    if(loc.indexOf('/bank')) {
        var splitit = loc.split('/bank');
        if(splitit[0] == 'http://10.10.10.99') {
            document.location = 'https://www.update.co.uk' + '/bank' + splitit[1];
        } else if(splitit[0] == 'https://www.update.co.uk') {
            document.location = 'http://10.10.10.32/' + '/bank' + splitit[1]; 
        } else if(splitit[0] == 'http://10.10.10.32/') {
            document.location = 'http://10.10.10.99' + '/bank' + splitit[1]; 
        } 
    }">Switch</a>

    And then got everyone in my team to go to this page and bookmark the link and it does the job swimmingly.

    Thanks
    Last edited by elduderino; Sep 19, 2008 at 10:07.


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
  •