admin管理员组

文章数量:1300069

Sorry I'm new in Javascript. I wanna get a number from my url in javascript, because I need it very much. I'd got this code from another link but it's not perfect enough for my case. The code is like this:

<html>
<head>
    <style>
    </style>
    <script type="text/javascript">
        function kampret(){
            x=location.pathname.split('/')[1];
            alert(x);
        }
    </script>
</head>
<body onload='kampret()'>
</body>
</html>

My url is like this:

.php?ai=85&ver=7

I want to get that number of '85', and i used that code and the result is ztest1.php not 85. And how do i get the only 85? Thank you.

Sorry I'm new in Javascript. I wanna get a number from my url in javascript, because I need it very much. I'd got this code from another link but it's not perfect enough for my case. The code is like this:

<html>
<head>
    <style>
    </style>
    <script type="text/javascript">
        function kampret(){
            x=location.pathname.split('/')[1];
            alert(x);
        }
    </script>
</head>
<body onload='kampret()'>
</body>
</html>

My url is like this:

http://domain./ztest1.php?ai=85&ver=7

I want to get that number of '85', and i used that code and the result is ztest1.php not 85. And how do i get the only 85? Thank you.

Share Improve this question edited Nov 11, 2015 at 8:36 Juna serbaserbi asked Nov 11, 2015 at 8:31 Juna serbaserbiJuna serbaserbi 2272 silver badges13 bronze badges 1
  • 9 Possible duplicate of How to get the value from the URL parameter? – Alex Andrei Commented Nov 11, 2015 at 8:32
Add a ment  | 

4 Answers 4

Reset to default 5

You can try

var getUrlParams = function (url) 
{
  var params = {};
  (url + '?').split('?')[1].split('&').forEach(
    function (pair) 
    {
       pair = (pair + '=').split('=').map(decodeURIComponent);
       if (pair[0].length) 
       {
         params[pair[0]] = pair[1];
       }
  });

  return params;
};

Result is

Object {ai: "85", ver: "7"}

Usage:

// 'http://domain./ztest1.php?ai=85&ver=7'
var params = getUrlParams( window.location.href );
alert( params.ai ); // 85

Try with this:

function getURLParameter(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}

$(document).ready(function(){
    alert(getURLParameter('ai'));
}); //JQuery

window.onload = alert(getURLParameter('ai')); //JavaScript

Adopted for yours:

<body onload='kampret()'></body>
<script>
    function getURLParameter(name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null
    }

    function kampret() {
        alert(getURLParameter('ai'));
    }
</script>

Try with this:

//var url = "http://domain./ztest1.php?ai=85&ver=7";
var url = window.location.href;
var idx = url.indexOf("?");
var idx2 = url.indexOf("&");
var hash = idx != -1 ? url.substring(idx+4, idx2) : "";

And if you want only the 85 in this code you can use hash = idx != -1 ? hash.substring(3, idx) : "";

A smaller way using php and javascript.

<?php $id = $_GET['ai']; ?>
<script language="javascript">
var id = <?php echo $id; ?>;
alert(id);
</script>

本文标签: phpHow do I get id number from url in javascriptStack Overflow