Forum Moderators: open
I am not very good when it comes to writing Javascript, however, I'm currently building a site and I have a problem. I have a page that can be accessed by two different pages (only images on the page) and I need to be able to identify which page the user has come from. This is so that I can then display an image specific to the page that they have come from in, conjuction with a generic image that will appear in both cases. I do not want to have two separate pages as this will result in maintenance overheads.
I thought about using the history.previous method but don't seem to be able to get the url of the page so that I can then identify which image to display.
In English, what I thought I could do was:
Use the onLoad event to capture the history.previous value, then use an if statement to determine which image to display. The images would be in <div> tags both with a 'style=display:none' statement so that they would be hidden.
Can anyone help me with the code that I need to write. Or advise me of a better way to do what I require?
Thanks in advance for any help.
There are several ways to do it. One, you can check the
document.referrer value .. which should contain the URL for the previous page. But, you can also use a query string when accessing the page. Instead of linking to
blah.html you can link to blah.html?thispage.html where thispage.html is the name of the refering page. Then you could just use the following JavaScript on the
blah.html page. <script type="text/javascript">
a=location.search.substring(1);
</script>
The
a variable will now contain the name of the previous page, and you can check it to hide/unhide images, put in backlinks .. etc Hope that little info helps :)
Then your script on the target page just extracts the passed variable and writes the appropriate image into the div.
I think this approach would be more dependable than assuming the window history will always give you the object you want. For instance, I often open links in a a new window.