JavaScript如何通过url传递变量

JavaScript how to Pass the variable by url?

本文关键字:变量 url 何通过 JavaScript      更新时间:2023-09-26

我想通过URL传递变量:http://localhost/new_wiki/test.php?id=http://example.com

我使用的是var first = getUrlVars()["id"];这行是传递值,但不起作用,请帮帮我。

test.php是这样的:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.js"></script> 
</head>
<body>
<div id="article"></div>

<script type="text/javascript">

    $(document).ready(function hiren(){
var first = getUrlVars()["id"];

    $.ajax({
        type: "GET",
        url: "first",
        contentType: "application/json; charset=utf-8",
        async: false,
        dataType: "json",
        success: function (data, textStatus, jqXHR) {
        var markup = data.parse.text["*"];
        var i = $('<div></div>').html(markup);
        // remove links as they will not work
        i.find('a').each(function() { $(this).replaceWith($(this).html()); });
        // remove any references
        i.find('sup').remove();
        // remove cite error
        i.find('.mw-ext-cite-error').remove();
        $('#article').html($(i).find('p'));

        },
        error: function (errorMessage) {
        }
    });    
    });


</script>
<h1>
</h1>


</body>
</html>

但是什么都没有发生它的错误是什么plz帮助我。

JS:

   $(document).ready(function hiren(){
        // On form's submit...
        $('form').submit(function(){
            // Get input's url
            var url = $('input[name="url"]').val();
            // Do ajax's GET request
            $.ajax({
                type: "GET",
                url: url, // <-- this is the url from the input
                contentType: "application/json; charset=utf-8",
                async: false,
                dataType: "json",
                success: function (data, textStatus, jqXHR) {
                    var markup = data.parse.text["*"];
                    var i = $('<div></div>').html(markup);
                    // remove links as they will not work
                    i.find('a').each(function() { $(this).replaceWith($(this).html()); });
                    // remove any references
                    i.find('sup').remove();
                    // remove cite error
                    i.find('.mw-ext-cite-error').remove();
                    $('#article').html($(i).find('p'));

                },
                error: function (errorMessage) {
                }
            });
        });
    });