简体   繁体   English

Javascript从外部网页获取文本

[英]Javascript get Text from an External Webpage

I have looked for a long time but I haven't been able to find a simple solution. 我已经寻找了很长时间,但找不到一个简单的解决方案。 On my webpage I want to input an address which will be looked up by a website which returns a webpage where in the text the username I need is located. 在我的网页上,我想输入一个地址,该地址将由一个网站查找,该网站返回一个网页,该网页的文本中包含我所需的用户名。

https://id.ripple.com/v1/user/ + the username inputed. https://id.ripple.com/v1/user/ +输入的用户名。 It would open the page like https://id.ripple.com/v1/user/rUkUchFYfvjucGtGDtSV2avQCzmJrktkVw where there will be a username parameter containing sashadkiselev. 它将打开类似于https://id.ripple.com/v1/user/rUkUchFYfvjucGtGDtSV2avQCzmJrktkVw的页面,其中将包含一个包含sashadkiselev的用户名参数。 I want to be able to make a function which takes the address opens that webpage retrieves the username and shows it in a pop up box. 我希望能够创建一个函数,该函数将打开地址以使网页检索用户名并将其显示在弹出框中。

As redundant as your request seems, if you are using jQuery, you can have the following code to do what you want: 尽管看起来很多余,但是如果您使用的是jQuery,则可以使用以下代码执行所需的操作:

$(function () {
    function getUsername(username, callback) {
        $.getJSON('https://id.ripple.com/v1/user/' + username, function (data) {
            callback.call(data, data.username);
        });
    }

    getUsername('sashadkiselev', function (username) {
        alert('Username is: ' + username);
        // the rest of the data is also available as "this"
    });
});

Note that the ability to pass a function as the callback parameter to getUsername() allows you to easily customise what you do with the username. 请注意,将函数作为callback参数传递给getUsername()功能使您可以轻松自定义对用户名的操作。

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

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