如何获得我的GET值?

时间:2015-05-22 14:46:04

标签: javascript get

当我转到此链接时:http://main.php?valretour=OK,如何获取valretour的值?

Main.php

我在我的PHP页面中直接尝试了这个...:

<script>
    alert($_GET['valretour']);
    if($_GET['valretour'] == "OK"){
        $.toaster({ priority : 'success', title : 'Success', message : 'OK' });
    } else {
        $.toaster({ priority : 'warning', title : 'Failed', message : 'NOT' });
    }
</script>

我的页面main.php

<html>
    <head>
        <!--Links css-->
        <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <script type='text/javascript' src='jquery/external/jquery/jquery.js'></script>
        <script type='text/javascript' src="jquery/jquery-ui.js"></script>
        <script src="jquery/jquery.toaster.js"></script>
    </head>

    <body>
        <div id="logo">
            <a href="#"><img src="images/logost.png" style="width:180px;height:120px"></a>
        </div>
        <script src="jquery/external/jquery/jquery.js"></script>
        <script src="jquery/jquery-ui.js"></script>

        <script>
            alert($_GET['valretour']);
                if($_GET['valretour'] == "OK"){
                    $.toaster({ priority : 'success', title : 'Success', message : 'OK' });
                } else {
                    $.toaster({ priority : 'warning', title : 'Failed', message : 'NOT' });
                }
            alert();
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

您需要解析location.search

var query = (function() {
    function decode(string) {
        return decodeURIComponent(string.replace(/\+/g, " "));
    }
    var result = {};
    if (location.search) {
        location.search.substring(1).split('&').forEach(function(pair) {
            pair = pair.split('=');
            result[decode(pair[0])] = decode(pair[1]);
        });
    }
    return result;
})();


if(query['valretour'] == "OK"){
    $.toaster({ priority : 'success', title : 'Success', message : 'OK' });
} else {
    $.toaster({ priority : 'warning', title : 'Failed', message : 'NOT' });
}