开发者

Getting input from user, building a URL and then opening it in current window

I need some javascript that will provide a textbox for the user to supply a key, append a parameter to a URL and then open it in the current window.

Simple, I'm sure, but I'm new to Javascript.

Any help would certainly b开发者_开发知识库e appreciated!

Thanks!


Depending on what action you want the user to take to trigger this, you could call this function which would accomplish the task.

HTML:

<input type="text" name="key" id="theTextBoxID" />

JS:

function changeLocation(){

    var theTextBox = document.getElementById("theTextBoxID");
    window.location = "someurl.html?someVariable="+theTextBox.value;

}

This function could be called by a submit action or an onclick event, etc...


If you had this HTML

<input type="text" name="url" id="url" />
<input type="submit" value="Open URL" onclick="open_url(); return false;" />

Then you would use this JavaScript

function open_url(){
   var url = document.getElementById('url');
   url = encodeURIComponent(url); // This escapes special characters

   window.location = 'http://yourdomain.com/?path=' + url; // Change the current url of the page
}

If you were redirecting to a page on the same server, you don't need the domain name or protocol in the window.location call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜