如何在JavaScript中获取URL参数?

huangapple go评论83阅读模式
英文:

How to get URL parameter inside javascript?

问题

我是新手学习JavaScript和PHP。

我尝试在JavaScript中发布带有URL参数的URL。我该如何实现?

这是我的页面URL:https://localhost/test/home.php?id=07

function showStackedVerticalChart() {
  { // 我想将上述URL参数添加到此URL中(id=07)
    $.post("partials/get-chart-data.php?chart_type=vertical-bar&id=07",
      function(data) {
        var chartdata = {
          labels: ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6", "Line 7", "Line 8", "Line 9", "Line 10",
            "Line 11", "Line 12", "Line 13", "Line 14", "Line 15", "Line 16", "Line 17", "Line 18", "Line 19", "Line 20",
            "Line 21", "Line 22", "Line 23", "Line 24", "Line 25", "Line 26", "Line 27", "Line 28", "Line 29", "Line 30", "Line 31", "Line 32"
          ],
          datasets: data
        };

        var graphTarget = $("#stacked-vertical-chart");

        var graph = new Chart(graphTarget, {
          type: 'bar',
          data: chartdata,
          options: {

            scales: {
              xAxes: [{
                barPercentage: 0.5,
                stacked: true
              }],
              yAxes: [{
                stacked: true
              }]
            }
          }
        });
      });
  }
}

我尝试将这段代码添加到脚本中。

$id = $_GET['id'];
$.post("partials/get-chart-data.php?chart_type=vertical-bar&' . $mo . '"
英文:

I'm new to JavaScript and PHP.

I'm trying to post url with URL parameter inside JavaScript. How can I achieve it?

Here is my page URL : https://localhost/test/home.php?id=07

<!-- language: lang-js -->

function showStackedVerticalChart() {
  { // I want to add above URL parameter to this URL (id=07)
    $.post(&quot;partials/get-chart-data.php?chart_type=vertical-bar&amp;id=07&quot;,
      function(data) {
        var chartdata = {
          labels: [&quot;Line 1&quot;, &quot;Line 2&quot;, &quot;Line 3&quot;, &quot;Line 4&quot;, &quot;Line 5&quot;, &quot;Line 6&quot;, &quot;Line 7&quot;, &quot;Line 8&quot;, &quot;Line 9&quot;, &quot;Line 10&quot;,
            &quot;Line 11&quot;, &quot;Line 12&quot;, &quot;Line 13&quot;, &quot;Line 14&quot;, &quot;Line 15&quot;, &quot;Line 16&quot;, &quot;Line 17&quot;, &quot;Line 18&quot;, &quot;Line 19&quot;, &quot;Line 20&quot;,
            &quot;Line 21&quot;, &quot;Line 22&quot;, &quot;Line 23&quot;, &quot;Line 24&quot;, &quot;Line 25&quot;, &quot;Line 26&quot;, &quot;Line 27&quot;, &quot;Line 28&quot;, &quot;Line 29&quot;, &quot;Line 30&quot;, &quot;Line 31&quot;, &quot;Line 32&quot;
          ],
          datasets: data
        };

        var graphTarget = $(&quot;#stacked-vertical-chart&quot;);

        var graph = new Chart(graphTarget, {
          type: &#39;bar&#39;,
          data: chartdata,
          options: {

            scales: {
              xAxes: [{
                barPercentage: 0.5,
                stacked: true
              }],
              yAxes: [{
                stacked: true
              }]
            }
          }
        });
      });
  }
}

<!-- end snippet -->

I tried adding this inside the script.

$id = $_GET[&#39;id&#39;];
$.post(&quot;partials/get-chart-data.php?chart_type=vertical-bar&amp;&#39;&quot; . $mo . &quot;&#39; &quot;

答案1

得分: 2

Rather than calling upon PHP to process and render a JavaScript variable, you can do it all in JavaScript - making use of the URLSearchParams API.

/*
  The URLSearchParams API allows access to the querystring
  - to get/set, etc., but does not parse the full URL so
  the URL needs to be pre-processed to create the 
  querystring.
  
  When using with the actual page URL, you can use
  location.search instead.
*/

let pageurl = 'https://localhost/test/home.php?id=07';
let [ url, query ] = pageurl.split('?');

let args = new URLSearchParams( query );
console.log( 'The ID from the URL string is:', args.get('id') );

To add that into the above function, you could do something like:

function showStackedVerticalChart() {
    let href = document.location.search();
    let args = new URLSearchParams(href);
    let id = args.has('id') ? args.get('id') : 0;

    $.post(`partials/get-chart-data.php?chart_type=vertical-bar&id=${id}`,
        function(data) {
            var chartdata = {
                labels: [
                    "Line 1", "Line 2", "Line 3", "Line 4", "Line 5", "Line 6", "Line 7", "Line 8", "Line 9", "Line 10",
                    "Line 11", "Line 12", "Line 13", "Line 14", "Line 15", "Line 16", "Line 17", "Line 18", "Line 19", "Line 20",
                    "Line 21", "Line 22", "Line 23", "Line 24", "Line 25", "Line 26", "Line 27", "Line 28", "Line 29", "Line 30", "Line 31", "Line 32"
                ],
                datasets: data
            };
            var graphTarget = $("#stacked-vertical-chart");
            var graph = new Chart(graphTarget, {
                type: 'bar',
                data: chartdata,
                options: {
                    scales: {
                        xAxes: [{
                            barPercentage: 0.5,
                            stacked: true
                        }],
                        yAxes: [{
                            stacked: true
                        }]
                    }
                }
            });
        }
    );
}
英文:

Rather than calling upon PHP to process and render a javascript variable you can do it all in Javascript - making use of the URLSearchParams api

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

/*
  The URLSearchParams api allows access to the querystring
  - to get/set etc but does not parse the full url so
  the url needs to be pre-processed to create the 
  querystring.
  
  When using with the actual page url you can use
  location.search instead.
*/

let pageurl=&#39;https://localhost/test/home.php?id=07&#39;;
let [ url, query ]=pageurl.split(&#39;?&#39;);


let args=new URLSearchParams( query );
console.log( &#39;The ID from the url string is:&#39;, args.get(&#39;id&#39;) );

<!-- end snippet -->

To add that into the above function you could do something like

function showStackedVerticalChart() {
	let href=document.location.search();
	let args=new URLSearchParams( href );
	let id=args.has(&#39;id&#39;) ? args.get(&#39;id&#39;) : 0;
	
	$.post(`partials/get-chart-data.php?chart_type=vertical-bar&amp;id=${id}`,
		function(data) {
			var chartdata = {
				labels: [
					&quot;Line 1&quot;, &quot;Line 2&quot;, &quot;Line 3&quot;, &quot;Line 4&quot;, &quot;Line 5&quot;, &quot;Line 6&quot;, &quot;Line 7&quot;, &quot;Line 8&quot;, &quot;Line 9&quot;, &quot;Line 10&quot;,
					&quot;Line 11&quot;, &quot;Line 12&quot;, &quot;Line 13&quot;, &quot;Line 14&quot;, &quot;Line 15&quot;, &quot;Line 16&quot;, &quot;Line 17&quot;, &quot;Line 18&quot;, &quot;Line 19&quot;, &quot;Line 20&quot;,
					&quot;Line 21&quot;, &quot;Line 22&quot;, &quot;Line 23&quot;, &quot;Line 24&quot;, &quot;Line 25&quot;, &quot;Line 26&quot;, &quot;Line 27&quot;, &quot;Line 28&quot;, &quot;Line 29&quot;, &quot;Line 30&quot;, &quot;Line 31&quot;, &quot;Line 32&quot;
				],
				datasets: data
			};
			var graphTarget = $(&quot;#stacked-vertical-chart&quot;);
			var graph = new Chart(graphTarget, {
				type: &#39;bar&#39;,
				data: chartdata,
				options: {
					scales: {
						xAxes: [{
							barPercentage: 0.5,
							stacked: true
						}],
						yAxes: [{
							stacked: true
						}]
					}
				}
			});
		}
	);
}

答案2

得分: 0

获取 PHP 代码块中的 id 值,如下所示:

<?php $id = $_GET['id']; ?>

然后在 JS 中输出 id 变量,如下所示:

$.post("partials/get-chart-data.php?chart_type=vertical-bar&id=<?php echo $id;?>"
英文:

First, get the id value in the PHP code block like

&lt;?php $id = $_GET[&#39;id&#39;]; ?&gt;

Then echo the id variable inside JS like this

$.post(&quot;partials/get-chart-data.php?chart_type=vertical-bar&amp;id=&lt;?php echo $id;?&gt;&quot;

huangapple
  • 本文由 发表于 2023年7月18日 13:54:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定