Click to See Complete Forum and Search --> : call External javascript from onclick


nogis
01-04-2006, 03:48 AM
I have a question i can't seem to find a solution to.

I have an external javascript, that is set to bring a user back to the last page they visited on my site.
The last visited URL is stored in a cookie.

Everything works well, only, i want the script to run on an onclick event.

This is the external javascript:

function readCookie(name)
{
var name = name+"="
var ca = document.cookie.split(';')
for(var i=0;i<ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length)
if (c.indexOf(name) == 0)
return (c.substring(name.length,c.length))


}
return null
}

if(readCookie('DSTS'))location=readCookie('DSTS')


This is how i attached the external javascript to my html:

<script LANGUAGE="javascript" src="cookie.js">
</script>


This is where i want the javascript to be triggered:

<a href="javascript:void(0);" onclick="readcookie();">back</a>


As for now, each time i open the page the javascript triggers straight away sending me back to my previous page immediately. :confused:

Can anyone help me out here please?

Thanks in advance

FromU2ME
01-04-2006, 04:26 AM
What is the value of DSTS?

if(readCookie('DSTS'))location=readCookie('DSTS')

nogis
01-04-2006, 05:59 AM
Do you mean the cookie code?:


function saveCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000))
var expires = "; expires="+date.toGMTString()


}


else expires = ""
document.cookie=name+"="+(value)+expires+"; path=/"

}


saveCookie('DSTS',location,10)

FromU2ME
01-04-2006, 06:08 AM
Nope, I meant the value => the URL that the user will be redirected to...

nogis
01-04-2006, 06:23 AM
This is an example of what's inside the cookie:


DSTS
file:///C:/Documents%20and%20Settings/rl/My%20Documents/New%20Blue%20Book/attrib/Section-Brand.htm
~~local~~/
1088
122733184
29759746
1599262736
29757734
*

FromU2ME
01-04-2006, 06:33 AM
Try if this works for you:

function readCookie(id) {

var url = "";
var ca = document.cookie.split(';');

for (var i=0; i < ca.length; i++) {
if (ca[i].split("=")[0] == id) { // Find right cookie by name
url = ca[i].split("=")[1];
break;
}
}

if (url != null) {
document.location.href = url;
}
else {
// If there's no url, move to main site
document.location.href = 'index.html';
}

}The rest of the code should stay intact :cool:

nogis
01-04-2006, 06:41 AM
i've loaded it onto my webserver, but when i click on the "back", nothing happends.

This is what's set in the cookie when i used it on the webserver:


DSTS
http://localhost:5050/attrib/Section-Brand.htm
localhost/
1088
1187896704
29759751
2667336256
29757739
*


(in the cookie it's all in one line)

FromU2ME
01-04-2006, 06:57 AM
Make this change:

<a href="#" onclick="readCookie();">back</a>Both my and your function had a capital C in their names.

nogis
01-04-2006, 07:04 AM
i've changed it, but i get a "page cannot be found" error somehow.

At least it's more then i had before ;)

Any idea why?

FromU2ME
01-04-2006, 07:08 AM
Perhaps you're being moved to 'index.html' and you don't have a file like that...?

Make this change to see what's wrong in the file name:


...

if (url != null) {
document.location.href = "javascript:alert(" + url + ");";
}
else {
// If there's no url, move to main site
document.location.href = 'index.html';
}

...Submit the text from the alert box if you can't figure out how to move on from there.

FromU2ME
01-04-2006, 07:11 AM
Just thought of one thing, what you're trying to do can be done with this simple function:

function doMove() {
document.location.href = document.referrer;
}

nogis
01-04-2006, 07:17 AM
i've changed the script, and now i receive an empty messagebox with an "alert" sign.
Was that what it was supposed to do?

nogis
01-04-2006, 07:19 AM
I've tried them before, but going back in history-urls like that is not an option.

The asp page needs to fully reload in order to have my password string etc. working well.

FromU2ME
01-04-2006, 07:23 AM
I forgot to change one thing with your code earlier... ;)
This must work!!! :cool:

cookie.js
function readCookie(id) {

var url = "";
var ca = document.cookie.split(';');

for (var i=0; i < ca.length; i++) {
if (ca[i].split("=")[0] == id) { // Find right cookie by name
url = ca[i].split("=")[1];
break;
}
}

if (url != "") {
document.location.href = url;
}
else {
document.location.href = 'index.html'; // Change to your default page
}

}

HTML-page:
<script language="javascript" type="text/javascript" src="cookie.js">

<a href="#" onclick="readCookie('DSTS');">back</a>

nogis
01-04-2006, 07:35 AM
yes yes yes yes!!!! It finally works!

You have no idea how much time i've spend changing things and searching for things in this script.

Thank you a thousand times!

I don't know if there's some kind of rewarding system here, but if so, let me know! you're worth a few rewards for sure!

Thanks ForU2ME, really

FromU2ME
01-04-2006, 07:39 AM
A simple "thanks" does it quite good :D