0

I am wondering how to take the information from a parsed query string and use it to display on the top of my page. Ignore the window.alert part of the code, I was just using that to verify that the function worked. For example: If the user had choices of Spring, Summer, Winter, and Fall, whichever they chose would display a a header on the next page. So if (seasonArray[i]) = Fall, I want to transfer that information into the form and display it as a element. I'm sure this is easily done, but I can't figure it out. Thanks, in advance.

function seasonDisplay() {
        var seasonVariable = location.search;
        seasonVariable = seasonVariable.substring(1, seasonVariable.length);
        while (seasonVariable.indexOf("+") != -1) {
            seasonVariable = seasonVariable.replace("+", " ");
        }
        seasonVariable = unescape(seasonVariable);
        var seasonArray = seasonVariable.split("&");
        for (var i = 0; i < seasonArray.length; ++i) {
        window.alert(seasonArray[i]);
        }
        if (window != top)
            top.location.href = location.href
    }
Bob
  • 91
  • 1
  • 1
  • 9
  • You want user input to affect the content of a separate page? Sounds like you need to either pass the user information to the server or use javascript to build the "next page." – Justin Aug 12 '17 at 21:08
  • I did pass the information to the next page. I created a submit button, and using the function as the onsubmit, and the alert box came up as season = fall. That was just a test. I don't want the alert box, I want the info to display as a header element – Bob Aug 12 '17 at 21:17
  • See [Toggle URL parameter with button](https://stackoverflow.com/q/31765968/) – guest271314 Aug 12 '17 at 21:25
  • Still not sure I am following completely, but started an answer that might be what you are looking for. – Justin Aug 12 '17 at 21:34

1 Answers1

0
<h1 id="DynamicHeader"></h1>

Replace the alert line with:

document.getElementById("DynamicHeader").insertAdjacentHTML('beforeend',seasonArray[i]);
Justin
  • 663
  • 7
  • 19
  • That is definitely the direction I'm trying to go, but it didn't work – Bob Aug 12 '17 at 21:42
  • What did it do? What's in your seasonArray? In your code example, are you getting just one pop up alert or multiple pop ups? – Justin Aug 12 '17 at 21:45
  • On page one, the user can click one of four radio buttons(summer, spring, winter, or fall). I stored that info in a query string. On page two, I parsed the query string with the above function. When I used an alert, the option that was selected popped up. (just 1 alert-with 1 selected option) – Bob Aug 12 '17 at 21:54
  • What did the console/error say? Without seeing more of your code, I can't test this myself. If seasonArray[1] sent proper alerts, then this line should instead add it to the `

    ` with the appropriate ID value. Leave the alert on at first to make sure it's still parsing properly, etc.

    – Justin Aug 12 '17 at 22:05
  • The alert still comes up correct, but no heading. Is there an alternative to insertAdjacentHTML? – Bob Aug 12 '17 at 22:16
  • `.innerHTML` but that isn't going to work right if it needs to reiterate over a loop. I'm thinking your loop is running only once, though... Or else you should be getting lots of pop ups. Confirm, you did make sure the html tag has the appropriate ID? Is your ` – Justin Aug 12 '17 at 22:29