PDA

View Full Version : Converting from embedded to external JavaScript problem with the "this keyword?"



MarcMiller
11-17-2006, 05:57 AM
Hi
some one gave me the script below which is almost exactly what I want if I could only converted to an external JavaScript file. The closest I have been able to come to doing this is to explicitly put the URL address fully written out between quotation marks where the variable name "URL" (in the below function) is passed the URL value just by using the"this.href" in the function call. I made several attempts to do this so I sure would appreciate some help. My reason for this want is this script is to be used twice on 27 pages.

Very sincerely
Marc



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>delay</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="txt/javascript">
<script type="text/javascript">
function goURL(url){
setTimeout(function(){window.location.href=url},100)
}
</script>
</head>
<body>
<a href="http://www.google.com"
onclick="goURL(this.href);return false">
mylink
</a>
</body>
</html>

Acey99
11-17-2006, 07:21 PM
remove .href with goURL(this.href) so it'll read goURL(this)

and in the function add url.href

function goURL(url){
setTimeout(function(){window.location.href=url.href},100)
}


that should work.

mwinter
11-17-2006, 11:03 PM
some one gave me the script below which is almost exactly what I want if I could only converted to an external JavaScript file. The closest I have been able to come to doing this is to explicitly put the URL address fully written out between quotation marks where the variable name "URL" (in the below function) is passed the URL value just by using the"this.href" in the function call. I made several attempts to do this so I sure would appreciate some help.

Your external file should contain:



function goURL(url) {
setTimeout('location.href = "' + url + '";', 100);
}

You would then call that as you do now:



<a href="http://www.google.com/" onclick="goURL(this.href); return false;">...</a>




<head>
<title>delay</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">

If you're going to use a meta element to indicate the encoding scheme, rather than HTTP headers (the proper place for that information), include it as the first child of the head element. The browser should be notified as soon as possible before it might encounter non-ASCII characters (which may reasonably occur in a title element).



<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="txt/javascript">

These two elements can be omitted. At the very least, fix the typo: text/javascript. :)




remove .href with goURL(this.href) so it'll read goURL(this)

Why? It doesn't significantly alter the behaviour of the code.



that should work.

The code already works. It's a bit daft, but it works.

Mike

MarcMiller
11-26-2006, 10:49 AM
Here's the conversion the way I actually achieved it. Thank you for those responses.

HTML


<!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>
<title>relevant snippets from page</title>
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta content="text/html; charset=us-ascii"
http-equiv="Content-Type" />
<script src="GaS.js" type="text/javascript"></script>
</head>
<ul>
<li id="prev">
<a href="GaS2.html">
<img src="p1images/prevpointer.gif" alt="prev"/>
</a>
</li>
<li id="return">
<a href="../gallery.html">
<img src="p1images/return.gif" alt="return" />
</a>
</li>
<li id="next">
<a href="GaS4.html">
<img src="p1images/nextpointer.gif" alt="next" />
</a>
</li>
</ul>
<embed src="click_x.wav" autostart=false width=0
height=0 id="sound1" enablejavascript="true" volume="30">


javaScript


function onClickDelayLocation(){
goURL(this.href);
return false;
function goURL(url){
onClickSound();
setTimeout(function(){window.location.href=url},100)
};
};
function onClickSound () {
var thissound=document.getElementById("sound1");
thissound.Play();
}
window.onload = function () {
document.getElementById("prev").getElementsByTagName('A')[0]
.onclick=onClickDelayLocation;
document.getElementById("return").getElementsByTagName('A')[0]
.onclick=onClickDelayLocation;
document.getElementById("next").getElementsByTagName('A')[0]
.onclick=onClickDelayLocation;
}