Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    864
    Thanks
    51
    Thanked 6 Times in 5 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)

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,348
    Thanks
    11
    Thanked 589 Times in 570 Posts
    take out the this.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 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).

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,348
    Thanks
    11
    Thanked 589 Times in 570 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    864
    Thanks
    51
    Thanked 6 Times in 5 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)

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 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".

  • #7
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    864
    Thanks
    51
    Thanked 6 Times in 5 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)

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 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:

    tpeck (01-11-2013)

  • #10
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    864
    Thanks
    51
    Thanked 6 Times in 5 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)

  • #11
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    864
    Thanks
    51
    Thanked 6 Times in 5 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)

  • #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    tpeck (01-11-2013)

  • #13
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    864
    Thanks
    51
    Thanked 6 Times in 5 Posts
    You are amazing! Thank you.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 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.
    Last edited by Old Pedant; 01-11-2013 at 11:52 PM.
    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:

    tpeck (01-12-2013)

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •