Welcome to WebmasterWorld Guest from 54.221.87.97

Forum Moderators: open

Message Too Old, No Replies

Javascript history object

history.previous question

     

delvsie

10:28 pm on Jan 30, 2003 (gmt 0)

10+ Year Member



Hi all,

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.

DrDoc

10:39 pm on Jan 30, 2003 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



delvsie,

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 :)

tedster

10:44 pm on Jan 30, 2003 (gmt 0)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You also might pass a variable in a query string at the end of the URL in the anchor on each tag, as simple as a "1" or a "2". So the anchor would be written <a href="newpage.html?im=1">link</a>

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.

delvsie

10:52 pm on Jan 30, 2003 (gmt 0)

10+ Year Member



DrDoc and tedster...

Thanks for the speedy reply. I will take on board what you have said.

Thanks again. :)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month