Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attach external .JS but wait for onclick to be triggered

Status
Not open for further replies.

Nogi

Technical User
Dec 10, 2004
132
BE
I have a question regarding a javascript that i have attached to my html.

Everything works well, except for the moment on which the javascript such function.

This is the problem:

I have an external javascript, set to launch the last page visited based upon a cookies information.

I've attached the .js to an html page like this:

Code:
<script LANGUAGE="javascript" SRC="cookie.js"></script>

What i want to do, is have the javascript execute when somebody presses the "back" text on the html.

What it does now is return to the previous page upon opening straight away.

How can i make this work the right way?
 
i have a text "back" on which i used to link the javascript:history.back() command. That i would like to replace with the external javascript. (if possible)
 
OK - so let me tell it as I understand it from your description.

You have a script, which will open the previously visited page. You want to go back a page in the browser's history, and then use the script to view the page before that - so in other words, you just want to go back 2 pages in the browser history?

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
nononono.

I have a page, let's say index.htm
That page contains a link "back", which is set to go to the previous page.

So far so good.

I used to go from index.htm to the previous page with <a href="javascript:history.back()">back</a>

I don't want to use that anymore.

Instead of that, i want to use an external .js that is set to bring me back to the last visited URL that is stored in a cookie.

So i thought i could simply do this to solve my problem
Code:
<a href="script src=cookie.js">back</a>

But that doesn't work.

So what i did was attach the file to the html in the header of my page like this:
Code:
<script LANGUAGE="javascript" SRC="cookie.js"></script>
But that makes the script execute directly when the index.htm is loaded.

I want it to work when the "back" text is clicked.

Uhm..i hope it's more clear now.
 
You can't set the src to be the js file (all the js file does is seperate your js functions from the actual page); you will have to give the function a name and call that function.

So, you need to do is create a function and give it a name then add it in the external js file. Then, on the onclick of your back button, call the relevant function (i.e. whatever name you have given to the function in your js file).



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
OK - what you want to do is a lot clearer, but we will not be able to help you with the automatic running without seeing the contents of your script file.

Chances are you do not have the code in a function, or you are calling it onliad - but without seeing, we can't know for sure which is the case.

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
oki, no problem i understand.

Look, this is in the external cookie.js:

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

So whenever the index.htm loads, this script executes immediately, which is not what i had in mind.

 
Move this line:

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

into a function:

Code:
function readCookie() {
   if(readCookie('DSTS'))location=readCookie('DSTS');
}

and then call that from your link:

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

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hey thanks for the help Dan.

I know receive an error though, saying "stack overflow at Line 19".

Any idea's where this might come from?
 
Oh sorry - my fault. I picked a function name that sounded good - but didn't see you had already used it!

In the code that I gave you, call my function something other than readCookie and you should be set!

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Again, thanks Dan, but i'm afraid it doesn't function anymore (or i have totally messed it up somehow, that's also possible hehe).

Look, this is the script of the cookie now:

Code:
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 
} 

function viewCookie() {
   if(readCookie('DSTS'))location=readCookie('DSTS');
}

This is in the header of my Index.htm-page:

Code:
<script LANGUAGE="javascript" SRC="cookie.js"></script>

This is in the body of the Index.htm-page:
Code:
<a href="javascript:void(0);" onclick="readCookie();">back</a> </td>

When i click on "back", nothing happends.

Do you have any idea what i might have done wrong?
 
I've changed the last command as you suggested Dan, but the result remains the same.

It doesn't do anything when i click on "back".

It seems that the script is not activated somehow.

You have any idea why?
 
I'd put an alert on the viewCookie function to know that getCookie is actually returning.

Cheers,
Dian
 
I checked everyting and the cookie is being made the right way.

It's a good idea to do the alert-thingy Dian.

But how do i put an alert in the function?
 
oki, i've tried it with the message in it Dan, but the message didn't show.

Maybe if i add the script that creates the cookie it might clear things up

The script that creates the cookie: right from within the html:

Code:
<script> 
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) 
</script>

This is the read cookie script, straight from within the Header of the HTML
Code:
<script LANGUAGE="javascript" type="text/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 
} 

function viewCookie() {
   alert('Your cookie contains:\n\n' + readCookie('DSTS'));
   if(readCookie('DSTS'))location=readCookie('DSTS');
}

This is the part that triggers the script, in the body of the html:
Code:
<a href="javascript:void(0);" onclick="viewCookie();">back</a>

That's all there's to it. Maybe somebody sees some mistake somewhere?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top