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.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Problem parsing javascript variables

    Hi,

    I have incounter a problem while parsing variables in javascript.

    Code:
    <form action="http://www.example.com" method="post">
    <input type="text" id="text" name="text" value="This is the problem">
    <input type="submit" name="submit" value="Submit">
    The variable is "This is the problem" but it returns as "This%20is%20the%20problem"...

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,030
    Thanks
    15
    Thanked 240 Times in 240 Posts
    Remember that %20 is just the escaped version of a space. You just need to unescape that block of text to recover the spaces.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    The %20 characters are not added by Javascript but are something to do with your server-side code.

    Never use the name/id submit, because then your JS code can't call document.formname.submit() if you ever need to.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 08-22-2014 at 05:27 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Quote Originally Posted by Philip M View Post
    The %20 characters are not added by Javascript
    True.
    but are something to do with your server-side code.
    False.

    They are added (substituted, actually) by the browser. It's called "URL Encoding".

    And the browser automatically encodes all form fields (whether you use method="GET" or method="POST") as the form submission is sent to the server.

    If the server simply echos those values--or if you use HTML to process a method="GET" submission--then indeed your JS code can see the encoded version.

    Now...the real question is WHY is Electrify seeing the %20 stuff in his JavaScript code???? I can't think of any kind of normal coding that would have JS seeing that.

    Or IS it in JavaScript code?
    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Old Pedant View Post

    False.

    They are added (substituted, actually) by the browser. It's called "URL Encoding".

    Now...the real question is WHY is Electrify seeing the %20 stuff in his JavaScript code???? I can't think of any kind of normal coding that would have JS seeing that.

    Or IS it in JavaScript code?


    Yes, sorry, slip of the keyboard. I should have said the browser.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New Coder
    Join Date
    Aug 2014
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post
    <script>
    function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&amp;");
    for (var i=0;i&lt;vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
    return pair[1];
    }
    }
    alert('Query Variable ' + variable + ' not found');
    }
    </script>
    //
    <script>
    alert( getQueryVariable(“x”) );
    </script>



    Above a simple example for parse javascript variables. U can study more about this by using online tutorials. then you can find the mistakes yourself.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Better than that:
    Code:
    <script>
    var querystring = [ ];
    if ( location.search.length > 1 )
    {
        var pairs = location.search.substring(1).split("&");
        for (var i=0; i < pairs.length; i++) 
        {
            var pair = pairs[i].split("=");
            querystring[ pair[0] ] = decodeURIComponent( pair[1] );
        }
    }
    
    ...
    alert( "query variable 'name' is " + querystring["name"] );
    Now you only decode the query once. And do *NOT* forget to use decodeURIComponent! Else a name such as "John Doe" will appear in your alert as "John%20Doe" or "John+Doe",
    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.

  • #8
    New Coder
    Join Date
    May 2013
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks for all the help

    Here is the code

    Code:
    <HTML><HEAD><TITLE>HTTP Traffic 1.5</TITLE></HEAD><BODY bgcolor="black"><center><br>
    <font color="white">Send artificial traffic to any URL. (6 Hits)</font>
    <br><br>
    	<script type="text/javascript">
    		site = function() {
    			var site_url = escape(document.getElementById('site_url').value);
    			if(site_url == "") {
    				alert('Type a url address!');
    				return false;
    			}
    		document.write("<TITLE>HTTP Traffic 1.5</TITLE>");
    		document.write("<body bgcolor='black'><font color='white'>Sending Hits to "+site_url+".<br> Wait 1 or 2 minutes...</font>");
    		document.write("<iframe frameborder='0' src='http://php-proxy.net/index.php?q="+site_url+"&hl=1e7' width='100%' height='100%'></iframe>");
    		}
    	</script>
    <input type="text" id="site_url">
    <input type="button" value="Send Hits" onclick="site()">
    <br><br><font color="white">Type an url including the http://</font>
    Last edited by Electrify; 09-02-2014 at 08:32 PM.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    It is this line:
    Code:
    var site_url = escape(document.getElementById('site_url').value);
    that is encoding the url to include %20 and any other %xx characters. The JS function escape( ) does that.

    That *is* the proper thing to do when passing a value in a URL's query string.

    So it looks to me like the code you show is doing the right thing.

    Personally, I would never never never have used document.write() like that, but you did and it (half-a.s.s.ed) works, so we'll let it go.

    Was there a question with your last post? Other than what I just answered?
    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.

  • #10
    New Coder
    Join Date
    May 2013
    Posts
    26
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Bit after bit i'm always learning

    There is a question that haven't been answered...

    How to fix

    Code:
    <font color='white'>Sending Hits to "+site_url+".<br> Wait 1 or 2 minutes...</font>
    And not showing the escape text
    Google
    http%3A//www.google.com

  • #11
    New Coder
    Join Date
    Sep 2010
    Location
    U S of A
    Posts
    67
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Quote Originally Posted by Electrify View Post
    Bit after bit i'm always learning

    There is a question that haven't been answered...

    How to fix

    Code:
    <font color='white'>Sending Hits to "+site_url+".<br> Wait 1 or 2 minutes...</font>
    And not showing the escape text
    Google
    http%3A//www.google.com
    You can "unDo" the escape with the unescape() function.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    But perhaps better is to only use escape() where it is needed. Even better is to *NOT* use obsolete escape() and instead use encodeURIComponent()

    Thus:
    PHP Code:
    site = function() {
        var 
    site_url document.getElementById('site_url').value// DO NOT USE escape()
            
    if(site_url == "") {
            
    alert('Type a url address!');
            return 
    false;
        }
        
    document.write("<TITLE>HTTP Traffic 1.5</TITLE>");
        
    document.write("<body   style=\"background-color: black; color: white;\">"
                    "Sending Hits to "
    +site_url+".<br> Wait 1 or 2 minutes...");
        
    document.write("<iframe frameborder='0' src='http://php-proxy.net/index.php?q="
                    
    encodeURIComponent(site_url
                    + 
    "&hl=1e7' width='100%' height='100%'></iframe>");

    But this is still a pretty terrible way to do all this, in my opinion.
    Last edited by Old Pedant; 09-06-2014 at 07:09 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:

    Electrify (09-14-2014)


  •  

    Posting Permissions

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