Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-11-2013, 09:23 AM
PM User |
#1
Regular Coder
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
redirect using javascript
I am trying to redirect a page to another using this code:
Code:
function GoToURL() {
var URLis = "http://google.com.au";
var location=(URLis);
this.location.href = location;
}
I felt sure it used to work. If conditions are right, you call the function GoToURL() and it changes the page to, in this case, google.
But now it doesn't work!
Is it obvious what is wrong?
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
01-11-2013, 10:31 AM
PM User |
#2
Senior Coder
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
take out the this .
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8%
IE10:10%
01-11-2013, 10:35 AM
PM User |
#3
Regular Coder
Join Date: Mar 2008
Location: London
Posts: 129
Thanks: 1
Thanked 31 Times in 31 Posts
I think location is the reserved keyword for JavaScript. Hence you can not use it as a variable name. Try to change the local variable name and also (remove this keyword).
01-11-2013, 10:53 AM
PM User |
#4
Senior Coder
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
Quote:
Originally Posted by
niralsoni
I think location is the reserved keyword for JavaScript. Hence you can not use it as a variable name. Try to change the local variable name and also (remove this keyword).
while poor practice, the use of var ensures that the name recycling is not the problem...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8%
IE10:10%
01-11-2013, 01:49 PM
PM User |
#5
Regular Coder
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
Thanks, I tried both ideas but no luck.
I have this:
Code:
<html>
<head>
<title>test</title>
<script type="text/javascript">
function check_form(){
alert("hello");
location.replace='http://google.com.au';
}
</script>
</head>
<body>
<form method="post" name="logonform" onsubmit="check_form()">
<table border="0" cellpadding="5" cellspacing="0">
<tr>
<td align="right">
<input type="image" src="img/login.gif" width="60" height="38" border="0">
</td>
</tr>
</table>
</form>
</body>
</html>
It only seems to function in Internet Explorer (9 I'm using).
Is something above not compatible with Chrome and FF?
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
01-11-2013, 03:15 PM
PM User |
#6
Regular Coder
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 946
Thanks: 7
Thanked 97 Times in 97 Posts
location.href = "http://google.com.au";
__________________
^_^
If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com ), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
01-11-2013, 09:25 PM
PM User |
#7
Regular Coder
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
But location.href doesn't work either.
I suspect that there is a problem trying to change the page from within the checking part of the form submit. Could that be it?
Does the form submit have to complete?
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
01-11-2013, 09:29 PM
PM User |
#8
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
AHA! See how it helps to show the *COMPLETE AND REAL* problem!!!
The culprit is *HERE*:
Code:
<form method="post" name="logonform" onsubmit="check_form()" >
Yes, your code is changing the page to google with
location.replace , but then that is being *OVERRIDDEN* by the form
submit !!!
Since you don't specify any
action= in your <form> tag, that means the the
submit will now take you RIGHT BACK TO THIS SAME PAGE!!!
If you do *NOT* want the submit action to happen, you *MUST* return
false from the
onsubmit=
So:
Code:
<form method="post" name="logonform" onsubmit="check_form(); return false; ">
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
01-11-2013, 09:32 PM
PM User |
#9
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
As an alternative:
Code:
<form method="post" name="logonform" onsubmit="return check_form()">
And then maybe something like
Code:
function check_form( )
{
if ( math.Random() < 0.5 )
{
location.href = "http://www.google.com.au";
return false;
}
return true;
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Users who have thanked Old Pedant for this post:
01-11-2013, 10:52 PM
PM User |
#10
Regular Coder
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
drive letter check
I have a script which determines the drive that a CD or DVD is playing from (obviously within Windows).
This little piece of code must reside on the disk in a file called drive.js:
Code:
var driveletter=a[i];
The driveletter variable is detected by this check:
Code:
<script>
function noErrorMessages () { return true; }
window.onerror = noErrorMessages;
var letter=false;
var a=new Array("B", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
var i=0;
</script>
<script src="file:///B:/drive.js"></script>
<script>i++;</script>
<script src="file:///D:/drive.js"></script>
<script>i++;</script>
<script src="file:///E:/drive.js"></script>
<script>i++;</script>
<script src="file:///F:/drive.js"></script>
<script>i++;</script>
<script src="file:///G:/drive.js"></script>
<script>i++;</script>
<script src="file:///H:/drive.js"></script>
<script>i++;</script>
<script src="file:///I:/drive.js"></script>
<script>i++;</script>
<script src="file:///J:/drive.js"></script>
<script>i++;</script>
<script src="file:///K:/drive.js"></script>
<script>i++;</script>
<script src="file:///L:/drive.js"></script>
<script>i++;</script>
<script src="file:///M:/drive.js"></script>
<script>i++;</script>
<script src="file:///N:/drive.js"></script>
<script>i++;</script>
<script src="file:///O:/drive.js"></script>
<script>i++;</script>
<script src="file:///P:/drive.js"></script>
<script>i++;</script>
<script src="file:///Q:/drive.js"></script>
<script>i++;</script>
<script src="file:///R:/drive.js"></script>
<script>i++;</script>
<script src="file:///S:/drive.js"></script>
<script>i++;</script>
<script src="file:///T:/drive.js"></script>
<script>i++;</script>
<script src="file:///U:/drive.js"></script>
<script>i++;</script>
<script src="file:///V:/drive.js"></script>
<script>i++;</script>
<script src="file:///W:/drive.js"></script>
<script>i++;</script>
<script src="file:///X:/drive.js"></script>
<script>i++;</script>
<script src="file:///Y:/drive.js"></script>
<script>i++;</script>
<script src="file:///Z:/drive.js"></script>
<script language="JavaScript">
window.onload=new function(){
document.cookie="yourdrive="+letter+'; path=/'}
</script>
Notice how it drops a cookie which contains the driveletter information.
The driveletter can be read back with this:
Code:
// use this script in those pages where you want the driveletter's value
var driveletter=false;
nCookie=document.cookie.split(";");
for(i in nCookie){
if(nCookie[i].indexOf("yourdrive")!==-1){
var driveletter=nCookie[i].substring(nCookie[i].indexOf("=")+1,nCookie[i].length);
}}
My question is, this all takes place in an html page.
Is there a way to have the driveletter check take place within an external .js file?
How can the bit below be done within a .js instead of html?
Code:
...
<script src="file:///D:/drive.js"></script>
<script>i++;</script>
<script src="file:///E:/drive.js"></script>
<script>i++;</script>
...
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
01-11-2013, 10:53 PM
PM User |
#11
Regular Coder
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
Gloom. I messed up.
This is meant to be in a new post. Sorry.
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
01-11-2013, 11:39 PM
PM User |
#12
Master Coder
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
Quote:
Originally Posted by
tpeck
How can the bit below be done within a .js instead of html?
Code:
...
<script src="file:///D:/drive.js"></script>
<script>i++;</script>
<script src="file:///E:/drive.js"></script>
<script>i++;</script>
...
Code:
var loadJS = function(nm) {
var s = document.createElement('script');
s.type='text/javascript';
s.src=nm;
document.getElementsByTagName('body')[0].appendChild(s);
}
loadJS("file:///D:/drive.js");
loadJS("file:///E:/drive.js");
i++;
Users who have thanked felgall for this post:
01-11-2013, 11:42 PM
PM User |
#13
Regular Coder
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
You are amazing! Thank you.
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
01-11-2013, 11:48 PM
PM User |
#14
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Since this is for windows, there is a much easier way
if you are willing to use MSIE only:
Code:
function findDriveThatIsPlaying( )
{
var drives = "BDEFGHIJKLMNOPQRSTUVWXYZ";
var fso = new ActiveXObject("Scripting.FileSystemObject");
for ( var d = 0; d < drives.length; ++d )
{
var driveletter = drives[d];
if ( fso.FileExists( driveletter + ":\\drive.js" ) )
{
document.cookie="yourdrive="+driveletter+"; path=/";
return driveletter;
}
}
// if you get here, no "drive.js" found on any drive
// what do you want to do???
return null;
}
I think that *HAS* to be *MUCH* faster than trying to get JavaScript to load a file and run it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 01-11-2013 at 11:52 PM ..
Users who have thanked Old Pedant for this post:
01-11-2013, 11:51 PM
PM User |
#15
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
I think Felgall missed the end of his answer.
Shouldn't it be:
Code:
var loadJS = function(nm) {
var s = document.createElement('script');
s.type='text/javascript';
s.src=nm;
document.getElementsByTagName('body')[0].appendChild(s);
}
var drives = "BDEFGHIJKLMNOPQRSTUVWXYZ";
for ( var d = 0; d < drives.length; ++d )
{
var driveletter = drives[d];
loadJS("file:///" + driveletter + ":/drive.js");
i++;
}
or similar???
But I still think that's not the best way, given that this is for Windows only, MSIE only.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 03:26 PM .
Advertisement
Log in to turn off these ads.