javascript 如何在 window.location.search 中搜索特定文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12126764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:23:40  来源:igfitidea点击:

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

javascript

提问by Samuel Bala

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

我有以下代码,我希望它仅在“?”之后的部分显示 div。包括“gclid=”。注意 url 可能看起来像 xxxxxx.com/?gclid=kljl3j4lk3j4l23

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

我不太确定如何合并部分字符串搜索,因此它只查找“?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

我对此有点陌生所以请原谅我的无知

回答by Gabe

You could use indexOf()

你可以用 indexOf()

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

回答by Jo So

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

回答by Vinod Srivastav

You can use javaScript splitto do that

您可以使用 javaScript split来做到这一点

Suppose you have a url like

假设你有一个像

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

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];

回答by hoogw

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); 

回答by Jarrett Meyer

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.

这两个都应该让你到达那里。