abhinav singh
abhinav singh

Reputation: 1104

How to add user input in a text box into the url to open a new webpage?

Please help me with this . My problem is that - I am trying to build a language recognization tool for my blog . I am currently weak in js so finding difficulties.

All i have to do is put a text box and ask user to input the language to detect . Then I have to pass the user input into the url , ie q="input from text box"

https://www.googleapis.com/language/translate/v2/detect?key=123&q="input from text box" please help.

What shoul i do with window.open() so that this happens?

Upvotes: 2

Views: 2467

Answers (2)

Joe
Joe

Reputation: 82654

I would use jQuery since you will need to use JSONP:

<div id="results"> </div>

....

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: 'https://www.googleapis.com/language/translate/v2/detect?key=123&q=' + $('inputId').val() + '&callback=?',
    success: function (data) {
          $.each(response.data.detections, function() {
            $('#results').text(JSON.stringify($(this)[0].language));
        })
    }
}); 

I'm ignoring the window.open because the response is JSON from that request. You can display it anyway you want.

Upvotes: 2

js1568
js1568

Reputation: 7032

<input type="text" id="lang" />

window.open('https://www.googleapis.com/language/translate/v2/detect?key=123&q='+document.getElementById('lang').value);

Upvotes: 4

Related Questions