www.webdeveloper.com
Results 1 to 14 of 14

Thread: [RESOLVED] Trouble getting "get" vars from URL

Hybrid View

  1. #1
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590

    resolved [RESOLVED] Trouble getting "get" vars from URL

    Does anyone know what's the problem with this javascript code here:

    function a() {
    var origionalurl = window.location
    var arraywithurl = origionalurl[1].split("=")
    var finalurl = arraywithurl[1]
    alert(finalurl)
    window.location = finalurl
    }


    I'm a begginer, so it's probably something very basic.

    What I'm trying to do is redirect someone to a URL in a variable called "loc" in the address bar. So, I get the starting URL (It would look something like "myURL?loc=another_URL") and split it into two parts ("myURL?loc" and "another_URL") and then tell the person a message and then redirect them to "another_URL". What's my problem?
    Last edited by shane.carr; 05-31-2006 at 07:36 PM. Reason: changed "java" to "javascript"

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    I guess you wanted:

    var arraywithurl = origionalurl.split("=")[1]

    the array is origionalurl.split("="), not origionalurl

  3. #3
    Join Date
    Dec 2004
    Posts
    8,637
    Quote Originally Posted by shane.carr
    Does anyone know what's the problem with this java code here:

    function a() {
    var origionalurl = window.location
    var arraywithurl = origionalurl[1].split("=")
    var finalurl = arraywithurl[1]
    alert(finalurl)
    window.location = finalurl
    }
    That isn't java code. That is JavaScript code. Java is something totally and separately different from JavaScript and, thus, the two should not be confused with one another.

    Otherwise... Besides the correction given in the previous post, this:

    var origionalurl = window.location

    should be this:

    var origionalurl = window.location.search.substr(1);

  4. #4
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590

    Arrow Update

    Thanks both of you! Some of it is fixed, but, the redirecting part still doesn't work. I do get a message with another_URL , but I'm not redirected to it. It redirects me to myURL with no get variables (see first post).

    Here is the code I have now:

    Code:
    function a() {
    	var origionalurl = window.location.search.substr(1);
    	var arraywithurl = origionalurl.split("=");
    	var finalurl = arraywithurl[1];
    	alert(finalurl);
    	window.location = finalurl;
    }
    What now?

  5. #5
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590
    If I change the last line to "redirect(finalURL)" or "window.redirect(finalURL)", it still doesn't work.

  6. #6
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590
    "document.location.href = finalURL" doesn't work either!

  7. #7
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590
    even if I put in something like "document.loaction.href = "http://www.google.com/" it still doesn't work! It redirects me to the just original (myURL) page!

  8. #8
    Join Date
    Dec 2004
    Posts
    8,637
    There must be more to your document that is causing that problem. If this:

    alert(finalurl);

    shows the correct URL, then this:

    window.location.href = finalurl;

    will definitely navigate to that URL -- all things being equal.

  9. #9
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590
    I know that it should, but it doesn't

  10. #10
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590
    I don't know, maybe it's something with my server. Is there any way to get the variables from javascript to html where I can redirect?

  11. #11
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590

    Arrow Full Code

    Here's the code from the entire document:

    HTML Code:
    <html>
    <script language="javascript">
    function a() {
    	var origionalurl = window.location.search.substr(1);
    	var arraywithurl = origionalurl.split("=");
    	var finalurl = arraywithurl[1];
    	alert(finalurl);
    	window.location.href = finalurl;
    }
    </script>
    <body>
    <form><input type="submit" value="Submit" onClick="a();"></form>
    </body>
    </html>
    Note: Right now, you have to click a submit button to activate the code. I'll make it on load later.

    Does this help?

  12. #12
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590
    Is there any other language that I could stick in there to make it redirect?

  13. #13
    Join Date
    Dec 2004
    Posts
    8,637
    Well, there's the problem right there. Change that submit button to a regular button and your code will work fine.

  14. #14
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590
    You're a genius! It worked!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles