0

I have html code like this:

<form action="getarrayjs.html" method="get">
<input type="text" id="text1" name="text1"/>
<input type="submit" id="sub1"/>
</form>

I need to pass the text1 value to getarrayjs.html page. That page code is like this:

<script>
var x = window.location.search;
document.getElementById("write").innerHTML = x;
</script>

<p id="write"></p>

But after submitting, nothing is appear on the getarrayjs.html page. What is the solution for this?

Dean Johns
  • 448
  • 3
  • 24
  • Are there any errors in the Javascript console? – Barmar Nov 23 '17 at 07:59
  • I'll bet there's an error saying you can't assign to `innerHTML` of `null`. See https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – Barmar Nov 23 '17 at 08:00
  • No any error in the output. Even its not saying Null – Dean Johns Nov 23 '17 at 08:13
  • 3
    Is your ` – Barmar Nov 23 '17 at 08:17
  • Yet it was. I corrected, now its working. Thank you! – Dean Johns Nov 23 '17 at 08:19

1 Answers1

0

With the window.location object. This code gives you GET without the question mark.

window.location.search.substr(1)

From your example it will return returnurl=%2Fadmin

EDIT: I took the liberty of changing Qwerty's answer, which is really good, and as he pointed I followed exactly what the OP asked:

function findGetParameter(parameterName) {
    var result = null,
        tmp = [];
    location.search
        .substr(1)
        .split("&")
        .forEach(function (item) {
          tmp = item.split("=");
          if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
        });
    return result;
}

I removed the duplicated function execution from his code, replacing it a variable ( tmp ) and also I've added decodeURIComponent, exactly as OP asked. I'm not sure if this may or may not be a security issue.

Or otherwise with plain for cycle, which will work even in IE8:

function findGetParameter(parameterName) {
    var result = null,
        tmp = [];
    var items = location.search.substr(1).split("&");
    for (var index = 0; index < items.length; index++) {
        tmp = items[index].split("=");
        if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
    }
    return result;
}

Original answer is here: https://stackoverflow.com/a/5448595/8248483

AND THEN:

FIRST CALL <p id="write"></p> and only then

<script> var x = window.location.search; document.getElementById("write").innerHTML = x; </script>

Lolipopcoder
  • 145
  • 9
  • His code just tries to display the entire `window.location.search`. If he gets nothing with that, how will parsing work? – Barmar Nov 23 '17 at 08:16
  • Use the `findGetParameter` function by entering Your `text1` key. – Lolipopcoder Nov 23 '17 at 08:18
  • His problem had nothing to do with getting the query parameters, it was because he was trying to display it in a DOM element that didn't exist yet. – Barmar Nov 23 '17 at 08:20
  • Still he will get the `?text1=some_word` but not only `some_word` without code. AND THE MAIN THING IS THAT @Ƥŕaśađ Şŕįɱał MUST FIRST CALL `

    ` and only then ``
    – Lolipopcoder Nov 23 '17 at 08:28
  • Exactly, that's what I said in my comments above. – Barmar Nov 23 '17 at 08:30
  • This question should just be closed as a duplicate of the question I linked to. I can't do that because I misunderstood it like you did and closed it as a duplicate of a question about parsing the URL parameters. – Barmar Nov 23 '17 at 08:30