开发者

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

开发者 https://www.devze.com 2022-12-11 22:26 出处:网络
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.

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

精彩评论

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