简体   繁体   English

如何使用 java 脚本从当前 url 获取多个 url 参数

[英]how to get multiple url parameters from current url using java script

I have 2 html pages, movies.html and single.html.我有 2 个 html 页面、电影。html 和单个.html。 I am passing few values from movies.html to single.html through href tag, so it goes along with the url and i can fetch it in single.html page. I am passing few values from movies.html to single.html through href tag, so it goes along with the url and i can fetch it in single.html page. Below is the code:下面是代码:

movies.html:电影.html:

    <a href="single.html?var1=abc&var2=xyz&var3=def">

So now after clicking on this href link in html page, single.html?var1=abc&var2=xyz&var3=def this whole thing comes in the url.所以现在点击 html 页面中的这个 href 链接后,single.html?var1=abc&var2=xyz&var3=def 整个事情都在 Z572D4E421E5E6B9BC111D815E8A02 中。 My question is how to get values of var1, var2 and var3 using javascript.我的问题是如何使用 javascript 获取 var1、var2 和 var3 的值。

I have used below code in single.html to fetch the parameters:我在 single.html 中使用了以下代码来获取参数:

  var movieLink1 = window.location.href.split('?var1=')[1];
  var movieLink2 = window.location.href.split('&var2=')[1];
  var movieLink3 = window.location.href.split('&var3=')[1];

output: output:

  movieLink1 = var1=abc&var2=xyz&var3=def
  movieLink2 = var2=xyz&var3=def
  movieLink3 = var3=def

expected output:预期 output:

movieLink1 = abc
movieLink2 = xyz
movieLink3 = def

I am not getting the expected output.我没有得到预期的 output。

Basically abc, xyz and def are movie downloading links in my application, so if i click on movieLink1 button in single.html movie with 400p resolution should download, movieLink2 button should download 780p resolution movie and movieLink3 button should download 1080p resolution movie.基本上 abc、xyz 和 def 是我的应用程序中的电影下载链接,所以如果我单击单个中的 movieLink1 按钮。应该下载 400p 分辨率的 html 电影,movieLink2 按钮应该下载 780p 分辨率的电影,movieLink3 按钮应该下载 1080p 分辨率的电影。 In mobile its working fine but in laptop clicking on any button its downloading 1080p resolution movie.在移动设备上它工作正常,但在笔记本电脑上点击任何按钮下载 1080p 分辨率的电影。 I am not getting what is the problem, If someone can help I will be very greatful for them.我不明白是什么问题,如果有人可以提供帮助,我将非常感激他们。

You can use URLSearchParams您可以使用URLSearchParams

const searchParams = new URLSearchParams(window.location.search);

// access var1
const var1 = searchParams.get("var1");

URLSearchParams recommended by wiesson is the smoothest solution if you only need to support modern browsers but not Internet Explorer.如果您只需要支持现代浏览器而不需要支持 Internet Explorer, wiesson推荐的 URLSearchParams 是最流畅的解决方案。

If you'd look for a solution that works in all browsers then you can use a match() method with a regex like /var{n}=(\w+)(\W|$)/ , the search result under index 1 of the capturing groups will contain the desired parameter value.如果您要寻找适用于所有浏览器的解决方案,则可以使用match()方法和/var{n}=(\w+)(\W|$)/之类的正则表达式,即索引 1下的搜索结果的捕获组将包含所需的参数值。

example:例子:

const movieLink1 = window.location.href.match(/var1=(\w+)(\W|$)/)[1]
const movieLink2 = window.location.href.match(/var2=(\w+)(\W|$)/)[1]
const movieLink3 = window.location.href.match(/var3=(\w+)(\W|$)/)[1]

output: output:

movieLink1 = abc
movieLink2 = xyz
movieLink3 = def

You can use this function to get the parameters可以使用这个function来获取参数

 const url= 'single.html?var1=abc&var2=xyz&var3=def'

    function getparams(url){
        const paramsobj={}

        if(url.includes('&')){
      const urlarray= url.split('?')
     const  parampairs =urlarray[1].split('&')
     parampairs.forEach(p => {
          const nwpair= p.split('=')
          paramsobj[nwpair[0]]=nwpair[1]
     });

    }
    if(!url.includes('&')){
        const urlarray= url.split('#')
          const keyvalue=urlarray[1].split('=')
        const arr=keyvalue
                let i=0
              while(i<arr.length+2) {
               key=keyvalue.shift()      
                value=keyvalue.shift()   
                paramsobj[key]=value
                      i++
              }    
    }
    return  paramsobj

    }
    params=getparams(url)
    console.log(params.var2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM