简体   繁体   English

如何获取URL中的variabel php到javascript

[英]How to get variabel php in url to javascript

I have a code javascript 我有一个代码javascript

$(document).ready(function() {

            //datatables
            table_pertanyaan = $('#table_pertanyaan').DataTable({ 

                "processing": true, //Feature control the processing indicator.
                "serverSide": true, //Feature control DataTables' server-side processing mode.
                "order": [], //Initial no order.

                // Load data for the table's content from an Ajax source
                "ajax": {
                    "url": "<?php echo site_url('pertanyaan/ajax_list/')?>/" + id,
                    "type": "POST"
                },

                //Set column definition initialisation properties.
                "columnDefs": [
                { 
                    "targets": [ -1 ], //last column
                    "orderable": false, //set not orderable
                },
                ],

            });

And I have a parameter in URL localhost/ci/edit/87 87 is id. 而且我在URL localhost/ci/edit/87有一个参数87是id。

How to I get variable from URL to use Javascript code?. 如何从URL获取变量以使用Javascript代码?

Due to this link, 由于这个连结,

So as an example if you had the following url with the javascript at the bottom in place. 因此,举个例子,如果您在以下网址的底部放置了javascript。

http://papermashup.com/index.php?id=123&page=home

All you'd need to do to get the parameters id and page are to call this: 获取参数ID和页面所需要做的就是调用此命令:

var first = getUrlVars()["id"];
var second = getUrlVars()["page"];

alert(first);
alert(second);

The Code 编码

function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}

you can also visit these links: 您还可以访问以下链接:

https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters

https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript

https://www.sitepoint.com/get-url-parameters-with-javascript/

There is a much better way to do this if you are using CodeIgniter. 如果使用的是CodeIgniter,则有更好的方法。

This is an answer specific to your question which requires a JavaScript answer. 这是特定于您问题的答案,需要JavaScript答案。

Assuming the url will always be having edit before id . 假设url将始终在id之前进行edit

var url_parts = window.location.pathname.split('/');
var id =  url_parts[url_parts.indexOf("edit")+1];

If you want the CodeIgniter solution, I suggest you search for it because its already answered or comment and I'll tell you. 如果您想要CodeIgniter解决方案,建议您搜索它,因为它已经回答或发表了意见,我会告诉您。

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

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