如何在扫描QR码后直接执行URL而不向用户显示

时间:2017-02-05 04:22:14

标签: javascript php cordova

正如标题所述,我正在使用QR扫描功能进行cordova应用。我目前的代码能够扫描我生成的QR码,将其存储到本地存储中,然后显示按下另一个按钮时存储的数据。这将导致嵌入在QR代码中的URL(存储在localhost上的.php文件)显示在屏幕上,但我必须点击它来执行URL(这将对数据库做一些事情)。

但是,我希望在QR扫描程序从QR代码中提取URL而不将其显示给用户之后执行URL。我在网上看了一下,但无法找到解决方案。如果有人可以提供帮助,我将不胜感激。

以下是QR扫描仪的代码:

<!DOCTYPE html>
<html>
    <head>
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <title></title>
    </head>
    <body>

        <div data-role="page" id="home">
            <div data-role="header">
                <h1></h1>
            </div>

            <div data-role="main" class="ui-content">
                <p>
                    <a target="_blank" href="javascript:scan();" style="text-decoration: none"><button>Scan</button></a>
                    <a target="_blank" href="#display" style="text-decoration: none"><button>Display Scanned Data</button></a>
                </p>
            </div>
        </div>

        <div data-role="page" id="display">
            <div data-role="header">
                <a target="_blank" href="#home" class="ui-btn ui-icon-home ui-btn-icon-left">Home</a>
                <h1>Display</h1>
            </div>

            <div data-role="main" class="ui-content">
                <table data-role="table" data-mode="column" id="allTable" class="ui-responsive table-stroke">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Value</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>

        <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <script>
            function scan()
            {
                cordova.plugins.barcodeScanner.scan(
                    function (result) {
                        if(!result.cancelled)
                        {
                            if(result.format == "QR_CODE")
                            {
                                navigator.notification.prompt("Please enter name of data",  function(input){
                                    var name = input.input1;
                                    var value = result.text;

                                    var data = localStorage.getItem("LocalData");
                                    console.log(data);
                                    data = JSON.parse(data);
                                    data[data.length] = [name, value];

                                    localStorage.setItem("LocalData", JSON.stringify(data));

                                    alert("Done");
                                });
                            }
                        }
                    },
                    function (error) {
                        alert("Scanning failed: " + error);
                    }
               );
            }

            $(document).on("pagebeforeshow", "#display", function() {
                $("table#allTable tbody").empty();

                var data = localStorage.getItem("LocalData");
                console.log(data);
                data = JSON.parse(data);

                var html = "";

                for(var count = 0; count < data.length; count++)
                {
                    html = html + "<tr><td>" + data[count][0] + "</td><td><a href='javascript:openURL(\"" + data[count][1] + "\")'>" + data[count][1] + "</a></td></tr>";
                }

                $("table#allTable tbody").append(html).closest("table#allTable").table("refresh").trigger("create");

            });

            function openURL(url)
            {
                window.open(url, '_blank', 'location=yes');
            }

            //initialize
            if(localStorage.getItem("LocalData") == null)
            {
                var data = [];
                data = JSON.stringify(data);
                localStorage.setItem("LocalData", data);
            }
        </script>
    </body>
</html>

0 个答案:

没有答案