Samuel Bala
Samuel Bala

Reputation: 159

How can I search window.location.search for specific text?

I have the following code and I want it to show the div only when the part after the "?" includes "gclid=". Note the url could look like xxxxxx.com/?gclid=kljl3j4lk3j4l23

I am not quite sure how to incorporate a partial string search so it only looks for "?gclid"

<script type="text/javascript"> 
      $(function(){
            if (window.location.search == "?gclid") {
        $('#new').show();
  } else {
        $('#new').hide();
  }
});

        </script>

I am a bit new to this so pardon my ignorance

Upvotes: 3

Views: 13551

Answers (5)

hoogw
hoogw

Reputation: 5525

Extract each parameter one by one, working code:

http://localhost:10/mapserver1/viewer/?config=viewer_simple1&url=https://maps2.dcgis.dc.gov/dcgis/rest/services/Zoning/MapServer&zoom=17&lat=38.917292&long=-77.036420

You could do:

        var ___zoom;
        var ___lat;
        var ___long;
        var ___basemap;

        var ___type;
        var ___url;
        var ___title;
        var ___opacity;


        /*
         *  if (value) { 
         *     
         *  }
         * 
         * will evaluate to true if value is not:

                null
                undefined
                NaN
                empty string ("")
                false
                0
         * 
         * 
         * 
         */


        if ( location.search.match(/zoom=([^&]*)/i) )
        {
             ___zoom = location.search.match(/zoom=([^&]*)/i)[1];
         }

        if ( location.search.match(/lat=([^&]*)/i) )
        {
           ___lat = location.search.match(/lat=([^&]*)/i)[1];
        }

        if (location.search.match(/long=([^&]*)/i))
        {
            ___long = location.search.match(/long=([^&]*)/i)[1];
        }

        if (location.search.match(/basemap=([^&]*)/i))
        {
            ___basemap = location.search.match(/basemap=([^&]*)/i)[1];
        }

        if (location.search.match(/type=([^&]*)/i))
        {
            ___type = location.search.match(/type=([^&]*)/i)[1];
        }

       if (location.search.match(/url=([^&]*)/i))
        {
            ___url = location.search.match(/url=([^&]*)/i)[1];
        }


        if (location.search.match(/title=([^&]*)/i))
        {
            ___title = location.search.match(/title=([^&]*)/i)[1];
        }

        if (location.search.match(/opacity=([^&]*)/i))
        {
            ___opacity = location.search.match(/opacity=([^&]*)/i)[1];
        }


        //console.log(location.search.match(/zoom=([^&]*)/i)[0]);   //    'zoom=17'
        //console.log(location.search.match(/zoom=([^&]*)/i)[1]);   //     '17'
        console.log(___zoom); 
        console.log(___lat); 
        console.log(___long); 
        console.log(___basemap); 

        console.log(___type); 
        console.log(___url); 
        console.log(___title); 
        console.log(___opacity); 

Upvotes: 0

Vinod Srivastav
Vinod Srivastav

Reputation: 4255

You can use javaScript split to do that

Suppose you have a url like

http://www.website.com/profile?var1=abc&var2=def&var3=ghi

//the url
var path = "http://www.website.com/profile?var1=abc&var2=def&var3=ghi";

//get all url parameters
var parameters = path.split("?")[1]; 

// get the parameters
var para1 = parameters.split("&")[0];
var para2 = parameters.split("&")[1];
var para3 = parameters.split("&")[2];

// get the parameter value
var para1var = para1.split("=")[1];
var para2var = para2.split("=")[1];
var para3var = para3.split("=")[1];

Upvotes: 0

Jo So
Jo So

Reputation: 26501

if (window.location.search.match(/[?&]gclid=/))

Upvotes: 1

Gabe
Gabe

Reputation: 50493

You could use indexOf()

if(window.location.search.indexOf("gclid=") > -1)

Upvotes: 6

Jarrett Meyer
Jarrett Meyer

Reputation: 19573

You can do this either with a Regular Expression or substring and text parsing.

var stringPart = window.location.search.substr(0, 6);
if (stringPart == "?gclid") { ...

or

var re = new RegExp(/^\?gclid/);
if (window.location.search.match(re)) { ...

Both of those should get you there.

Upvotes: 0

Related Questions