CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   redirect using javascript (http://www.codingforums.com/showthread.php?t=285609)

tpeck 01-11-2013 09:23 AM

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?

rnd me 01-11-2013 10:31 AM

take out the this.

niralsoni 01-11-2013 10:35 AM

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).

rnd me 01-11-2013 10:53 AM

Quote:

Originally Posted by niralsoni (Post 1305458)
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...

tpeck 01-11-2013 01:49 PM

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?

WolfShade 01-11-2013 03:15 PM

location.href = "http://google.com.au";

tpeck 01-11-2013 09:25 PM

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?

Old Pedant 01-11-2013 09:29 PM

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;">

Old Pedant 01-11-2013 09:32 PM

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;
}


tpeck 01-11-2013 10:52 PM

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>
...


tpeck 01-11-2013 10:53 PM

Gloom. I messed up.

This is meant to be in a new post. Sorry.

felgall 01-11-2013 11:39 PM

Quote:

Originally Posted by tpeck (Post 1305586)
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++;


tpeck 01-11-2013 11:42 PM

You are amazing! Thank you.

Old Pedant 01-11-2013 11:48 PM

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.

Old Pedant 01-11-2013 11:51 PM

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.


All times are GMT +1. The time now is 09:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.