Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-11-2013, 09:23 AM   PM User | #1
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
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)
tpeck is offline   Reply With Quote
Old 01-11-2013, 10:31 AM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
take out the this.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 01-11-2013, 10:35 AM   PM User | #3
niralsoni
Regular Coder

 
Join Date: Mar 2008
Location: London
Posts: 129
Thanks: 1
Thanked 31 Times in 31 Posts
niralsoni is an unknown quantity at this point
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).
niralsoni is offline   Reply With Quote
Old 01-11-2013, 10:53 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by niralsoni View Post
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.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 01-11-2013, 01:49 PM   PM User | #5
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
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)
tpeck is offline   Reply With Quote
Old 01-11-2013, 03:15 PM   PM User | #6
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 01-11-2013, 09:25 PM   PM User | #7
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
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)
tpeck is offline   Reply With Quote
Old 01-11-2013, 09:29 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-11-2013, 09:32 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
tpeck (01-11-2013)
Old 01-11-2013, 10:52 PM   PM User | #10
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
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)
tpeck is offline   Reply With Quote
Old 01-11-2013, 10:53 PM   PM User | #11
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
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)
tpeck is offline   Reply With Quote
Old 01-11-2013, 11:39 PM   PM User | #12
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by tpeck View Post
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++;
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
tpeck (01-11-2013)
Old 01-11-2013, 11:42 PM   PM User | #13
tpeck
Regular Coder

 
tpeck's Avatar
 
Join Date: Oct 2002
Location: Sydney, Australia
Posts: 771
Thanks: 40
Thanked 5 Times in 4 Posts
tpeck is on a distinguished road
You are amazing! Thank you.
__________________
The difference between genius and stupidity is that genius has its limits. (Albert Einstein)
tpeck is offline   Reply With Quote
Old 01-11-2013, 11:48 PM   PM User | #14
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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..
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
tpeck (01-12-2013)
Old 01-11-2013, 11:51 PM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:28 PM.


Advertisement
Log in to turn off these ads.