Store query string data into variable for use in conditional

I have the first part, but I can't get the rest.

I have "captured" the values ​​of the query string. Now I need to store these values ​​in variables and then use the specified variables to call functions via a conditional expression.

Here's a fiddle .

Html

<div class="container">

    <div id="redBlue" class="welcome">
        <h2>Which Pill Will You Choose?</h2>
        <div class="buttonWrapper">
            <a href="#?choice=truth" class="button medium queryString">Red</a>
        <a href="#?choice=ignorance" class="button medium queryString">Blue</a>
        </div> <!-- /end buttonWrapper -->
    </div> <!-- /end welcome -->
    <div style="clear:both;"></div>

</div> <!-- /end container -->

      

Js

$(".queryString").click(function() {
    var query_string = this.href.substring(this.href.indexOf("=") + 1);
    // alert(query_string);
});

var red = query_string.valueOf("truth");
var blue = query_string.valueOf("ignorance");

if (query_string) {

    // Click red, then call truth() function

} 
else {

    // Click blue, then call ignorance() function

}

// Below are placeholders for the truth() and ignorance() functions.
function truth() { 
    // This function lives in an external .js file
}

function ignorance() {
   // This function lives in an external .js file 
}

      

I have tried: val()

, value()

, valueOf()

, var red = query_string === "truth";

and several other ways that I can not remember, because I zharena.

I am assuming that the changed variables change every time the buttons are pressed. Is it correct?

I need to capture "truth" or "ignorance" when clicked, pass these values ​​into variables, use them in a conditional expression that will run functions.

Let me illustrate the logic:

onClick get query string value
var red = query string value == "red"
var blue = query string value == "blue"

if (query string) {
     ...value equals red
     run function truth()
}
else {
     ...value equals blue
     run function ignorance()
}

      

Functions called in real time in an external JS file. These functions make REST API calls to SharePoint and create data tables based on list items (FYI only).

I would like to keep it as simple as possible. Note that the query string values ​​were written without RegEx.

+3


source to share


2 answers


All you need to compare a string to some other string is to use ==

or ===

. You need to do this work inside the "click" handler; your code, as written, will do what it does before the "click" handler is executed.



$(".queryString").click(function() {
    var query_string = this.href.substring(this.href.indexOf("=") + 1);
    if (query_string == "truth")
      truth();
    else if (query_string == "ignorance")
      ignorance();
});

      

+3


source


You have to convert String and compare both values ​​after



if (String(query_string) === String("truth"))
      truth();
else if (String(query_string) === String("ignorance"))
     ignorance();
      

Run codeHide result


-2


source







All Articles