Skip to content Skip to sidebar Skip to footer

Build URL From Two Form Fields

Essentially, we have two form fields asking for users to supply us with two alphanumeric strings, and a URL needs to be constructed from the two. However, there is a basic structur

Solution 1:

You pretty much answered your question.

HTML

<input id='UserEntry1' type='text'>
<input id='UserEntry2' type='text'>

Javascript

var URLBase = "http://URL.com/fixeddata1";
var TrailingFixedData = "fixeddata2";

finalURL = URLBase + document.getElementById('UserEntry1').value + TrailingFixedData + document.getElementById('UserEntry2').value;

Or if you're using jQuery:

finalURL = URLBase + $('#UserEntry1').val() + TrailingFixedData + $('#UserEntry2').val();

Solution 2:

http://jsfiddle.net/4M3q3/

HTML:

<input id="one"><input id="two">
<button id="open">Open</button>

JS:

$('#open').click(function() {
    var fixedData1 = 'http://www.google.com/#q=',
        fixedData2 = '+',
        userEntry1 = $('#one').val(),
        userEntry2 = $('#two').val();

    var newWindow = window.open(fixedData1 + userEntry1 + fixedData2 + two, '_blank');
    newWindow.focus();
});

Post a Comment for "Build URL From Two Form Fields"